How to find which method 'hangs' with async/await?

匆匆过客 提交于 2019-12-05 09:03:41

In situations like this, what you can do is go to the debug dropdown menu, go to Windows, and choose the "Tasks" window (The default short cut key combo is "Ctrl+D, K").

This can give you a clue on what tasks are hanging.

Look for tasks that have abnormally long Duration values, that is a indication that something happened to the task and it is not completing. If you double click on the line it will take you to the await that is hung.

I don't know for sure why await BurnHeretics(); does not show up in the list for me, but I do know that this window will behave differently depending on your OS version because it relies on OS features to track some types of tasks. But at minimum this will show you that await DoHolyWarComplicatedDetails(); is hanging which will lead you to inspect DoHolyWarComplicatedDetails() which will lead you to inspect BurnHeretics(); which will lead you to your bug that is causing the hang.

UPDATE: I just realized it does show await BurnHeretics(); as the main thing causnig the block. If you look at the Task column, the <DoHolyWarComplicatedDetails>d__3 means "in the method DoHolyWarComplicatedDetails the compiler generated class <DoHolyWarComplicatedDetails>d__3 is scheduled and is waiting for a signal to arrive." the <DoHolyWarComplicatedDetails>d__3 is the state machine for await BurnHeretics();, you can see it if you use a decompiler like DotPeek and allow show compiler generated code.

[CompilerGenerated]
private sealed class <DoHolyWarComplicatedDetails>d__3 : IAsyncStateMachine
{
  public int <>1__state;
  public AsyncTaskMethodBuilder <>t__builder;
  private TaskAwaiter <>u__1;

  public <DoHolyWarComplicatedDetails>d__3()
  {
    base..ctor();
  }

  void IAsyncStateMachine.MoveNext()
  {
    int num1 = this.<>1__state;
    try
    {
      TaskAwaiter awaiter;
      int num2;
      if (num1 != 0)
      {
        awaiter = MainWindow.BurnHeretics().GetAwaiter();
        if (!awaiter.IsCompleted)
        {
          this.<>1__state = num2 = 0;
          this.<>u__1 = awaiter;
          MainWindow.<DoHolyWarComplicatedDetails>d__3 stateMachine = this;
          this.<>t__builder.AwaitUnsafeOnCompleted<TaskAwaiter, MainWindow.<DoHolyWarComplicatedDetails>d__3>(ref awaiter, ref stateMachine);
          return;
        }
      }
      else
      {
        awaiter = this.<>u__1;
        this.<>u__1 = new TaskAwaiter();
        this.<>1__state = num2 = -1;
      }
      awaiter.GetResult();
      awaiter = new TaskAwaiter();
      Console.WriteLine("Heretics burned");
    }
    catch (Exception ex)
    {
      this.<>1__state = -2;
      this.<>t__builder.SetException(ex);
      return;
    }
    this.<>1__state = -2;
    this.<>t__builder.SetResult();
  }

  [DebuggerHidden]
  void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
  {
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!