What can I do to make my C# application take advantage of multiple processor cores?
Asked Answered
G

4

6

I've been doing some tests about the way a .NET C# application uses resources, like CPU or memory. I wrote some loops that calculate values for a large amount of numbers and I'm satisfied with the weight of the algorithm.

I have a Quad Core 2.4 GHz processor, but I've noticed that In Task Manager, my application is only using 25% of my CPU. Why isn't it using 100%? Does that mean that a .NET C# Application compiled in VS 2008 only supports Single Core CPU? Or is there a way that I can force it to use all CPUs?

Gorilla answered 11/1, 2011 at 12:5 Comment(2)
How long does it take your code to run now? Are you sure you need the extra speed? It might make your program significantly more complicated.Conjunction
You've really got to design your application with multiple core usage in mind though; plan out the tasks in your application which are CPU intensive and see if you can cut them down to asynchronous operations.Copy
G
1

What about if i use Background-workers ,if i have CPU QuadCore 4CPU's can somebody write an example how could it react @ that CPU ?

Gorilla answered 11/1, 2011 at 13:19 Comment(1)
Yes, background workers will run in separate threads and will allow more than 1 core of CPU utilization. You would need a total of 4 threads to use 4 cores. The hard part is figuring out how best to break your task apart into separate jobs.Conjunction
M
5

It all depends on how you can modify your code to use more than one core.

Unless you are doing parallel or multi threaded operations, then the programme won't use more than one core.

.NET 4 has a library that can help you: Parallel LINQ. For more information see this page: http://msdn.microsoft.com/en-us/library/dd997425.aspx

Middleton answered 11/1, 2011 at 12:15 Comment(0)
S
2

It will run at 1 core unless you specifically start some threading.

The easiest way is to push a method onto the ThreadPool.

   System.Threading.ThreadPool.QueueUserWorkItem(DoSomething);

   void DoSomething() { ... }
Snoopy answered 11/1, 2011 at 12:14 Comment(0)
C
2

Use .NET 4.0 TaskLibrary which is designed for benefiting from multi-core processors.

Carding answered 11/1, 2011 at 12:16 Comment(0)
G
1

What about if i use Background-workers ,if i have CPU QuadCore 4CPU's can somebody write an example how could it react @ that CPU ?

Gorilla answered 11/1, 2011 at 13:19 Comment(1)
Yes, background workers will run in separate threads and will allow more than 1 core of CPU utilization. You would need a total of 4 threads to use 4 cores. The hard part is figuring out how best to break your task apart into separate jobs.Conjunction

© 2022 - 2024 — McMap. All rights reserved.