如果从静态构造函数启动并等待空的 .NET 任务,为什么不会完成?
我不明白为什么下面的代码不起作用:
var task = new Task(() => { });
task.Start();
if (task.Wait(10000))
{
logger.Info("Works");
}
else
{
logger.Info("Doesn't work");
}
超时到期后,任务状态停留在“正在运行”,尽管没有什么可做的。不过,用 task.RunSynchronously()
替换 task.Start()
是可以的。
有谁知道我可能做错了什么?
此处提供了用于复制该问题的测试项目:http://erwinmayer.com/dl/TaskTestProject.zip< /a>.据我所知,如果上述代码的方法在静态构造函数中运行,它就不起作用。但如果直接作为静态类方法调用,它就可以工作。
最近的这篇 MSDN 博客文章似乎强调了静态构造函数的相关问题: http://blogs.msdn.com/b/pfxteam/archive/2011/05/03/10159682.aspx
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的上下文非常重要。当您启动这样的任务时,它会使用当前的调度程序 - 如果假设它将能够使用当前线程,那么当您等待它时,您将实际上陷入死锁。
在不同的上下文中使用相同的代码就可以了。
其他人说它对他们有用但对你不起作用的原因是,毫无疑问你在与其他人不同的上下文中运行此代码 - 但你没有向我们展示一个简短但完整的 /em> 程序,只是这个片段,所以每个人都试图以不同的方式重现它。 (我看到您现在上传了一个项目,这无疑会带来更多启发。当然,可以在问题中发布的简短但完整的程序通常是更好的选择。)
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.)
感谢大家的评论,它帮助我更加具体并最终隔离了问题。
我在这里创建了一个测试项目:http://erwinmayer.com/dl/TaskTestProject.zip。它表明我的问题中的代码如果在静态构造函数中运行则不起作用。但如果在静态构造函数初始化后直接作为静态类方法调用,它确实可以工作。
这篇最近的 MSDN 博文提供了一些技术见解关于处理多线程和静态构造函数时存在的相关问题:
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:
它有效:
并按预期输出
yes
。您一定在做其他事情导致它不起作用。在给定的形式中,没有你正在做什么/在哪里做的上下文,它就可以工作。即使这个令人厌恶的东西也有效:
It works:
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: