-1

By using Task.Run or Task.Factory.StartNew we can convert synchronous actions to tasks, so that we can use await, like this:

await Task.Run(() => { SomeMethod(); }

In the meantime, many methods themselves have asynchronous implements, so it is recommended to directly use

await SomeMethodAsync();

But what's the difference between the two?

Licc
  • 1
  • 4
    Possible duplicate of [When correctly use Task.Run and when just async-await](http://stackoverflow.com/questions/18013523/when-correctly-use-task-run-and-when-just-async-await) – Aron Aug 29 '16 at 08:59
  • The difference is that the first one will be executed in a context you control and the second one will be executed in a context defined by the implementation, which can be anything (simple task, pipeline, ...) – Sir Rufo Aug 29 '16 at 09:39

1 Answers1

2

SomeMethodAsync is probably a method that does IO work and IO work does not require a thread to work. So SomeMethodAsync does not cause a thread from the thread-pool to just sit and wait for it to complete. Thread-pool threads are important resources in server application such as ASP.NET applications. In such applications, each request is serviced by a thread-pool thread and thus the number of active requests can be increased by saving such threads.

await Task.Run(() => { SomeMethod(); } uses a thread-pool thread to execute the SomeMethod method. If the SomeMethod does IO work, then you just used a thread-pool thread unnecessarily.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • 1
    Please don't feed the dups. We've all linked to Stephen Cleary's blog post enough times... People really should be finding it on their first click by now. – Aron Aug 29 '16 at 09:00
  • If you have a look in the c# reference https://msdn.microsoft.com/en-us/library/hh156528.aspx you will see that the exemple SomeMethodAsync just wrap a task in a sub. And nothing force task to use a thread-pool. That's just the default behavior... You answer is not wront but it isn't really right either. – Marco Guignard Aug 29 '16 at 09:09
  • @Aron, although the question you referenced is related, I think it is not an exact duplicate. – Yacoub Massad Aug 29 '16 at 09:18
  • @MarcoGuignard, if you use `Task.Run` a thread pool thread will be used. I am not sure I understand your point. – Yacoub Massad Aug 29 '16 at 09:19
  • @YacoubMassad I refuse to believe there is no canonical answer. If there is not, I would like to nominate Stephen Cleary to write one. Perhaps by copy pasta his own blog.... – Aron Aug 29 '16 at 09:19
  • @YacoubMassad His point is that `Task.Run` is implemented by a thread pool thread under the ".net Framework". There is no guarantee that going forward .net core will use a thread pool thread, nor any other implementation (such as mono) use a thread pool thread. Nor is there any requirement in the specification for Microsoft to use the same implementation in future...Nor does it use a Threadpool thread if you set the LongRunning flag... – Aron Aug 29 '16 at 09:21
  • @Aron, the documentation of `Task.Run` says `Queues the specified work to run on the thread pool and returns a Task object that represents that work.` – Yacoub Massad Aug 29 '16 at 09:24
  • @YacoubMassad Task use a Taskscheduler object to control the handling. You could override it to make the start execute on the UI thread, a queue on only secondary thread or whatever you want. Tasks are a high-concept which use threadpool "by default" but task are more than that. – Marco Guignard Aug 29 '16 at 09:31
  • @MarcoGuignard, right. But this is not the point of the question. OP is using the simplest override of `Task.Run` and is asking about calling a natively `async` method vs running a synchronous method via `Task.Run`. – Yacoub Massad Aug 29 '16 at 09:35