Reputation: 39
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,
Upvotes: 3
Views: 3139
Reputation: 4911
You should use Task.Delay method.
private async void button1_Click(object sender, EventArgs e)
{
await test();
}
private Task test()
{
return Task.Delay(100000);
}
Upvotes: 2
Reputation: 457197
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.
Upvotes: 5