Hi
I'm working with .NET new TPL library and faced with some strange behavior for me I cannot explain. For some reason nested task is not started in my case. I've simplified the solution I have to the following:
bool flag = false;
for (int i = 0; i < 5; i++)
{
Task.Factory.StartNew(() =>
{
while (true) // a lot of newcoming tasks
{
Thread.Sleep(200); //do some work
Task.Factory.StartNew(() =>
{
flag = true;
});
}
});
}
Thread.Sleep(2000);
Assert.IsTrue(flag);
I have 5 Tasks that running concurrently. Each tasks retrieve some elements from pending queue, performs some operation and then try to run nested task for the results of this operation. The problem is that if there are too many elements (while(true) simulates this) and all 5 tasks are constantly running nested tasks are not started. The can only be started after most of tasks with while loop finish their execution.
It seems something wrong with while statements that blocks nested tasks to run, but I don't know what :)