Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

await still blocks current UI thread

I tried to use the following code to run some task in thread pool:

private async void button1_Click(object sender, EventArgs e)
{
   await test().ConfigureAwait(continueOnCapturedContext: false);
}
private Task test()
{
   Thread.Sleep(100000);
   return null;
}

The code is supposed to run in the threadpool, however the current UI thread is still being blocked.

So can anyone help to take a look? thanks,

like image 458
HPPPY Avatar asked Mar 16 '26 17:03

HPPPY


1 Answers

The code is supposed to run in the threadpool

No, that is not at all true. async does not run your code on a thread pool thread.

I suggest you read my async intro, which explains what async does do, and the official async FAQ, which addresses the thread pool misconception specifically:

Does the “async” keyword cause the invocation of a method to queue to the ThreadPool? To create a new thread? To launch a rocket ship to Mars?

No. No. And no.

like image 134
Stephen Cleary Avatar answered Mar 18 '26 07:03

Stephen Cleary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!