Where do async methods run?

mehmet6parmak

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?

dcastro

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using async to run multiple methods

From Dev

How do a library provide async methods if it is a bad practice to do Task.Run?

From Dev

Where are FIle.ReadAll***Async/WriteAll***Async/AppendAll***Async methods?

From Dev

Async methods that do not need cancellation

From Dev

How do static methods run?

From Dev

How do static methods run?

From Dev

Where to put methods that run when app starts?

From Dev

Do you have to await async methods?

From Dev

Why do unawaited async methods not throw exceptions?

From Dev

Where do BIRT scripts run?

From Dev

Where do BIRT scripts run?

From Dev

is asynchronous version of relaycommand required in order to run async methods correctly

From Dev

How to run async methods inside OnElementChanged method in a custom renderer

From Dev

How to get C# async methods to actually run in parallel?

From Dev

Where do methods of an object go in MVC?

From Dev

Task.Run vs. direct async call for starting long-running async methods

From Dev

Do Begin/End async socket methods (APM) exists on .net standard?

From Dev

Where do I run the setspn command?

From Dev

Where do I run logcat from?

From Dev

Where do I put commands to run scripts?

From Java

How do I get a warning in Visual Studio when async methods don't end in 'Async'?

From Dev

Are there methods or compatibility libraries to run .deb applications on Puppy linux (where where no app source is provided)?

From Dev

Where do I go to find the current contents of the async queue?

From Java

Do you have to put Task.Run in a method to make it async?

From Dev

Does let!/do! always run the async object in a new thread?

From Dev

async methods calling sync methods

From Dev

Where do methods defined at the top-level exist?

From Dev

How do I document methods where providing a block is optional?

From Dev

How/where do TreeNode subclasses implement Product-related methods?

Related Related

  1. 1

    Using async to run multiple methods

  2. 2

    How do a library provide async methods if it is a bad practice to do Task.Run?

  3. 3

    Where are FIle.ReadAll***Async/WriteAll***Async/AppendAll***Async methods?

  4. 4

    Async methods that do not need cancellation

  5. 5

    How do static methods run?

  6. 6

    How do static methods run?

  7. 7

    Where to put methods that run when app starts?

  8. 8

    Do you have to await async methods?

  9. 9

    Why do unawaited async methods not throw exceptions?

  10. 10

    Where do BIRT scripts run?

  11. 11

    Where do BIRT scripts run?

  12. 12

    is asynchronous version of relaycommand required in order to run async methods correctly

  13. 13

    How to run async methods inside OnElementChanged method in a custom renderer

  14. 14

    How to get C# async methods to actually run in parallel?

  15. 15

    Where do methods of an object go in MVC?

  16. 16

    Task.Run vs. direct async call for starting long-running async methods

  17. 17

    Do Begin/End async socket methods (APM) exists on .net standard?

  18. 18

    Where do I run the setspn command?

  19. 19

    Where do I run logcat from?

  20. 20

    Where do I put commands to run scripts?

  21. 21

    How do I get a warning in Visual Studio when async methods don't end in 'Async'?

  22. 22

    Are there methods or compatibility libraries to run .deb applications on Puppy linux (where where no app source is provided)?

  23. 23

    Where do I go to find the current contents of the async queue?

  24. 24

    Do you have to put Task.Run in a method to make it async?

  25. 25

    Does let!/do! always run the async object in a new thread?

  26. 26

    async methods calling sync methods

  27. 27

    Where do methods defined at the top-level exist?

  28. 28

    How do I document methods where providing a block is optional?

  29. 29

    How/where do TreeNode subclasses implement Product-related methods?

HotTag

Archive