Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 608
Rosmarine Popcorn Avatar asked Jan 11 '11 12:01

Rosmarine Popcorn


2 Answers

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

like image 195
Matt Ellen Avatar answered Sep 27 '22 23:09

Matt Ellen


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() { ... }
like image 31
Henk Holterman Avatar answered Sep 27 '22 23:09

Henk Holterman