'await' works, but calling task.Result hangs/deadlocks

前端 未结 5 1779
旧巷少年郎
旧巷少年郎 2020-11-21 23:56

I have the following four tests and the last one hangs when I run it. Why does this happen:

[Test]
public void CheckOnceResultTest()
{
    Assert.IsTrue(Check         


        
5条回答
  •  迷失自我
    2020-11-22 00:36

    Acquiring a value via an async method:

    var result = Task.Run(() => asyncGetValue()).Result;
    

    Syncronously calling an async method

    Task.Run( () => asyncMethod()).Wait();
    

    No deadlock issues will occur due to the use of Task.Run.

提交回复
热议问题