I have a small project - WinForms On .net frameWork - just a small test :
private void button9_Click(object sender, EventArgs e)
{
string text = GetTitleAsync().Result;
button9.Text = text;
}
private async Task<string> GetTitleAsync()
{
await Task.Delay(3000);
return "Hello!";
}
As I ran the application , Clicking the button: "button9" - caused a dead lock, (since the thread was hung on the ".result" )
Writing GetTitleAsync() this way:
private async Task<string> GetTitleAsync()
{
await Task.Delay(3000).ConfigureAwait(false);
return "Hello!";
}
solved the deadlock - and the application ran ok.
But I don't understand how ?
I would have expected, that using ".ConfigureAwait(false)" would cause a situation in which :
"button9.Text = text;" is executed on a different thread than the one, on which the UI was created, and an excpetion would be throwed !
but it works excellent ! how??
.ConfigureAwait(false);
from there, change:private async void button9_Click
andstring text = await GetTitleAsync();
- JimiConfigureAwait(false);
causes the continuation to resume on a different Thread, but theWindowsFormsSynchronizationContext
is strong, the first await will resume in the UI Thread. So will doResult
. But you don't block on async code. - Jimi