2

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?

2
  • By "async methods" do you mean methods with the async keyword or methods which return a Task that can be awaited on? I understand it's the latter but wanted to clarify. Commented Dec 6, 2013 at 14:39
  • methods with async keyword on their signatures Commented Dec 6, 2013 at 14:43

2 Answers 2

4

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:

  1. Thread A again - if there is a SynchronizationContext that enforces this (as in a WPF or WinRT application). Simply put, this is done by queuing and scheduling the remaining code (i.e., the third instruction) to be executed back on thread A.
  2. On a completely different thread B.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Tasks are executed on ThreadPool” I think that's a confusing formulation. As you explain in the first part of the answer, code for Task-returning async methods often isn't executed on the ThreadPool.
@svick Nice catch. The Method in my first snippet returns a compiler-generated Task which doesn't necessarily run on the ThreadPool. The Task manually created in my last snippet however, will definitely run on the ThreadPool. I've updated my answer.
3

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.

If you haven't already, check out my async intro post, which explains the threading behavior of async and await.

When you invoke an async method, it executes synchronously up to the first time it awaits an incomplete operation. The async method then captures the current "context" and schedules the rest of itself to execute later, and returns.

Later, when the operation completes, the rest of the async method is scheduled to run on that captured "context".

The "context" is the current SynchronizationContext unless it is null, in which case it is the current TaskScheduler. When you call an async method from the UI thread, then you have a UI SynchronizationContext, which works by posting work to the UI thread's message loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.