I aspect the following methods distributing threads to multicore processors for the execution of tasks, however, in Windows Task Manager, it showed only utilizing one particular core only. How to make it parallel for those 5 threads?
private void ActivateProcess
{
...
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker1));
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker2));
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker3));
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker4));
ThreadPool.QueueUserWorkItem(new WaitCallback(Worker5));
}
...
private void Worker1(Object stateInfo)
{
try
{
//for (int i = 0; i < _NumberOfLoop; i++)
Parallel.For(0,(long)_NumberOfLoop, i =>
{
if (_ForceToStop)
throw new System.ArgumentException(
"Force stop at WorkerThread 1", "Aborted");
// Process: Process a tasks
...
});
}
catch (Exception ex)
{
...
}
}
Hard to say from this code. You even got 2 layers of parallelism, so “it ought to work”.
The answer must be in
// Process: Process a tasks– it probably hangs on a common, non shared resource.If the code in the ‘tasks’ is independent and CPU intensive you should see all your cores get busy. But when you for instance try to mock it with Sleep() you won’t see anything happen in TaskManager.