I have read many articles about async-await pattern but still I am not sure whether the async methods (the awaited methods) run on the UI thread or not. I always end up with SynchronizationContext class "async methods run in the same SynchronizationContext with the callee" what does this exactly mean?
Is it a separate thread newly created (I know it is not) or a ThreadPool thread?
When I read SynchronizationContext, I see that it's some kind of queued tasks executor but where are these tasks executed? on ThreadPool?
If yes, async methods are executed on ThreadPool, right?
A method marked as async
runs on the thread on which it was called until it reaches the first await
keyword within it.
There is one exception though: if the method being awaited has already finished running by the time you reach the await
keyword, then execution simply carries on without switching threads.
public async Task Method()
{
Console.WriteLine(1);
await Something();
Console.Writeline(2);
}
In this case, if the method is called on thread A, then 1 would be printed from thread A as well.
Then you move onto the next instruction. If the Task
returned by something
has already completed, the execution continues on thread A. If the Task
returned by something
is still running, then 2 could be printed from either:
Regarding your doubts about the ThreadPool:
As we have seen, async
methods are not guaranteed to run on the ThreadPool. Only work scheduled by one of the Task.Run
overload methods will surely run on the ThreadPool. Quoting the Task
MSDN doc:
Queues the specified work to run on the ThreadPool and returns a task handle for that work.
public void Task Something()
{
return Task.Run(() => Console.WriteLine(3));
}
3 will be printed by a thread on the ThreadPool.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments