I'm trying to achieve parallel processing using System.Threading.Tasks.Task
class. Strangely, task delegate execution is halted at the first method call. I coded below sample code to recreate the issue.
using System;
using System.Threading.Tasks;
namespace TestApp
{
public static class TestClass
{
static TestClass()
{
var tasks = new Task[Environment.ProcessorCount];
for (int i = 0; i < Environment.ProcessorCount; i++)
{
var task = Task.Run(() =>
{
Console.WriteLine("Start the Task " + i);
// Method Foo is not called.
// Stack trace show a managed to native transition.
// Why?
Foo(i);
});
tasks[i] = task;
}
Task.WaitAll(tasks);
Console.WriteLine("Press eny key to exit");
Console.ReadKey();
}
private static void Foo(int i)
{
Console.WriteLine("Foo in the Task " + i);
}
}
}
I call the Method TestClass.Bar()
to invoke the static constructor of TestClass
.
Main thread waits for the parallel tasks at the Task.WaitAll(tasks)
call as expected. But it never complete because the tasks itself are stuck at the Foo method call.
Stack trace of one of the stuck tasks:
TestApp.exe!TestApp.TestClass..cctor.AnonymousMethod__0
[Managed to Native Transition]
TestApp.exe!TestApp.TestClass..cctor.AnonymousMethod__0() Line 18
mscorlib.dll!System.Threading.Tasks.Task.InnerInvoke()
mscorlib.dll!System.Threading.Tasks.Task.Execute()
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution)
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Can someone suggest why the method Foo does not get called?