0

I am still struggling to fully understand async/await approach. Can anyone tell me as in topic does await really does Task.Run behind? Sometimes people doing async metod including await but saw sometimes people do Task.Run without async method. What is the the right way?

Dino
  • 79
  • 9
  • 5
    No, it does not. You would probably find [There is no Thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html) by Stephen Cleary helpful. – Kirk Woll May 20 '17 at 18:16

4 Answers4

1

No, async await is just made to allow code to run whilst something else is blocking, and it doesn't do Task.Run, or start a new thread.

https://blog.stephencleary.com/2013/11/there-is-no-thread.html is a decent explanation of it.

gmn
  • 4,199
  • 4
  • 24
  • 46
0

The async operator simply turns on the use of the await operator in a method. The await operator will work on anything which is awaitable. In .NET, Task and Task<T> are some examples of awaitables.

Task.Run returns Task and Task.Run<T> returns Task<T> and as I said they are both awaitable so you can await on them.

You can even write your own awaitable type if you want: You just have to make sure to satisfy the requirements of what is considered an awaitable. An awaitable needs to provide an implementation for GetAwaiter and that's it. Here is an awaitable example:

public struct SimpleInt32Awaitable 
{ 
    public SimpleInt32Awaiter GetAwaiter() 
    { 
        return new SimpleInt32Awaiter(); 
    } 
}

public struct SimpleInt32Awaiter 
{ 
    public bool IsCompleted { get { return true; } }

    public void OnCompleted(Action continuation) 
    { 
    }

    public int GetResult() 
    { 
        return 5; 
    } 
}

And now you can do this:

// Within some other class 
static async void SimpleWaitAsync() 
{ 
    SimpleInt32Awaitable awaitable = new SimpleInt32Awaitable(); 
    int result = await awaitable; 
} 
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

does await really does Task.Run behind?

Others have already linked to one of Cleary's many excellent blog posts on C# concurrency, There Is No Thread.

No, it doesn't use Task.Run behind the scenes.

await is useful for more than just Task.Run. It's for running code after something has completed. That something could be a computation task created with Task.Run, but it could also be, say, data-retrieval, such as with ReadLineAsync. Task is the class used to model all of those somethings.

Task.Run is used to queue work to run on the thread-pool. This is appropriate when writing CPU-bound code, but you shouldn't use Task.Run when dealing with IO-bound operations. In that case, your asynchronous code (perhaps using async/await) should make use of asynchronous IO methods such as WriteLineAsync and the aforementioned ReadLineAsync.

It's an anti-pattern to use Task.Run to run blocking code on a worker thread. You never want to do that, even if it does, say, unblock your GUI thread.

Sometimes people doing async metod including await but saw sometimes people do Task.Run without async method. What is the the right way?

You can indeed create a Task using Task.Run and just let it run, without using await or ContinueWith. These are called 'fire and forget' tasks. They're rarely a good idea. Unlike when await is used, any exception thrown by that task will be lost.

This is explained by -- you guessed it -- Stephen Cleary, in this StackOverflow answer.

Community
  • 1
  • 1
Max Barraclough
  • 214
  • 2
  • 6
-1

There are cases where you are restricted from using async and await - this is, generally, why you will sometimes see Task.Run used instead - this allows you to utilize async code in a synchronous method.

An example of where this is necessary is when using MVC with Child Actions - the framework does not support async child actions, so you must call async code using Task.Run.

Chris Thompson
  • 490
  • 5
  • 17