简体   繁体   English

Task.Run冻结

[英]Task.Run freezing

Can anyone educate me why the following freezes after completing the lambda: 谁能教我为什么在完成lambda之后以下内容会冻结:

        return await Task.Run(() =>
        {
            return SuperLongMethod();
        })

...yet the following works as expected: ...但是,以下工作可以按预期进行:

        return Task.Run(() =>
        {
            return SuperLongMethod();
        }).GetAwaiter().GetResult();

It seems the Task.Run in the first version doesn't recognize that the lambda has completed. 似乎第一个版本中的Task.Run无法识别lambda已完成。 I have other examples in my code where I use the first version without issue, so I'm at a loss to understand what the difference is. 我的代码中还有其他示例,其中我使用第一个版本没有问题,因此我很茫然地了解它们之间的区别。

For clarity, SuperLongMethod() makes a web service call and does a lot of post-processing of the resultant data 为了清楚起见,SuperLongMethod()进行了Web服务调用,并对结果数据进行了大量的后处理

I'm guessing that further up your call stack you're calling Task.Wait or Task<T>.Result and thus causing the deadlock issue I describe on my blog . 我猜想在您的调用堆栈的最Task.Wait您正在调用Task.WaitTask<T>.Result ,从而导致我在博客上描述的死锁问题 You should use await instead. 您应该改用await

As a side note, SuperLongMethod should probably be broken up into a CallWebServiceAsync and DoPostProcessing , and only the DoPostProcessing should be wrapped in a Task.Run . 附带说明一下, SuperLongMethod应该可以分解为CallWebServiceAsyncDoPostProcessing ,而只有DoPostProcessing应该包装在Task.Run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM