Winforms call to async method hangs up program

前端 未结 1 1391
醉梦人生
醉梦人生 2020-12-14 15:16

I have been working around this problem for a while, but now I would really like to understand what goes wrong. I have a rather simple application (it\'s a turtoise SVN plug

相关标签:
1条回答
  • 2020-12-14 15:55

    Your problem is because you are blocking the UI thread when you call .Result and you told the continuation after Task.Delay to run on the UI thread. So you are blocking the UI waiting for a task that is blocked on waiting for the UI to become free, a classic deadlock.

    Two solutions. First make the button click async too.

    private async void buttonOk_Click(object sender, System.EventArgs e)
    {
        var asyncResolvedIssue = api.ResolveIssue(issue, revision, pathList);
        if (await asyncResolvedIssue) {} // <== no deadlock!
    }
    

    Event handlers are the only place you are allowed to do async void.

    The other option is tell Task.Delay it does not need to have the rest of its function run on the UI thread by setting ConfigureAwait(bool) to false.

    public async Task<bool> ResolveIssue(Issue issue, int revision, string[] pathList)
    {
        await Task.Delay(1000).ConfigureAwait(false);
    
        return true;
    }
    

    Now the line of code after the Task.Delay will run on a threadpool thread instead of the UI thread and will not be blocked by the fact that the UI thread is currently blocked.

    0 讨论(0)
提交回复
热议问题