Код: Выделить всё
private async void Button_Click(object sender, EventArgs e)
{
var result = DoWork(); // supposed to be async
MessageBox.Show("Done!");
}
private async Task DoWork()
{
// Simulate long work
Thread.Sleep(5000);
return "Finished!";
}
I expected the UI to stay responsive, but instead the window locks up for 5 seconds.
Why is await not preventing the freeze here?
What is the correct way to make sure the UI thread doesn’t block?
Should I be using Task.Run, ConfigureAwait(false), or something else?
What’s the recommended pattern for writing async code that keeps the UI responsive?
Код: Выделить всё
await
Подробнее здесь: https://stackoverflow.com/questions/797 ... o-i-fix-it