Why does my C# async method deadlock when called from UI thread?
I'm trying to use await inside a Windows Forms button click event, but the UI freezes indefinitely. I suspect a deadlock caused by ConfigureAwait(false). Can anyone explain what's happening and how to fix it?
12
The deadlock occurs because the UI thread is synchronously waiting for the async operation to complete. Use await all the way up, or call .ConfigureAwait(false) on the awaited task if you don't need to resume on the UI context.
8
Wrap the async call in Task.Run if you must keep the event handler synchronous, but it's better to make the click handler async:
private async void button1_Click(object sender, EventArgs e)
{
await DoWorkAsync();
}
5