I cannot understand why the following code will not work:
var task = new Task(() => { });
task.Start();
if (task.Wait(10000))
{
logger.Info("Works");
}
else
{
logger.Info("Doesn't work");
}
The task status is stuck on "Running" after the timeout expires, although there is nothing to be done. Replacing task.Start()
with task.RunSynchronously()
will work however.
Does anyone have an idea of what I may be doing wrong?
A test project to replicate the issue is available here: http://erwinmayer.com/dl/TaskTestProject.zip. As far as I can see, it doesn't work if the method with the above code runs within the static constructor. But it works if called directly as a static class method.
This recent MSDN blog post seems to highlight related issues with static constructors: http://blogs.msdn.com/b/pfxteam/archive/2011/05/03/10159682.aspx