I cannot understand why the following code will not work:
var task = new Task(() => { });
task.Start();
if (task.Wait(10000))
{
logger.Info(\"Works\");
Thank you everyone for your comments, it helped me be more specific and finally isolate the issue.
I created a test project here: http://erwinmayer.com/dl/TaskTestProject.zip. It shows that the code in my question doesn't work if it runs within the static constructor. But it does work if called directly as a static class method, after the static constructor has been initialized.
This recent MSDN blog post provides some technical insight on the existence of related issues when dealing with multithreading and static constructors:
The context is very important here. When you start a task like this, it uses the current scheduler - and if that assumes it will be able to use the current thread, you'll effectively deadlock when you wait for it.
The same code in different context would be fine.
The reason others are saying it's working for them, but it's not working for you, is that no doubt you're running this code in a different context to other people - but you haven't shown us a short but complete program, just this snippet, so everyone is trying to reproduce it in a different way. (I see you've now uploaded a project, which will no doubt shed more light. A short but complete program which could be posted in the question is generally preferable, of course.)
It works:
var task = new Task(() => { });
task.Start();
if (task.Wait(10000))
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
And gives the output yes
as expected. You must be doing something else which is causing it to not work. In the given form, without context of what / where you are doing it, it works.
Even this abomination works:
var task = new Task(() =>
{
var task1 = new Task(() =>
{
});
task1.Start();
if (task1.Wait(10000))
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
});
task.Start();
if (task.Wait(10000))
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}