Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Task.Yield, Task.Run, and ConfigureAwait(false)?

As I understand it, Task.Yield at the beginning of a method will force the caller to continue if it is not awaiting the method. Meanwhile Task.Run and ConfigureAwait(false) both run a Task on a new thread pool thread, which again will force the caller to continue if it is not awaiting the method.

I can't understand the difference between Task.Yield and running a new thread pool thread, since right after it returns to the caller, it will continue executing the rest of the method, which is essentially the same thing.

This post suggests that Yield and Task.Factory.StartNew (which is really just the old version of Task.Run) can be used interchangeably, which seems confusing to me.

like image 766
James Ko Avatar asked Jul 04 '15 15:07

James Ko


2 Answers

Task.Yield is not a replacement for Task.Run and it has nothing to do with Task.ConfigureAwait.

  • Task.Yield - Generates an awaitable that completes just after it is checked for completion.
  • ConfigureAwait(false) - Generates an awaitable from a task that disregards the captured SynchronizationContext.
  • Task.Run - Executes a delegate on a ThreadPool thread.

Task.Yield is different than ConfigureAwait in that it is an awaitable all by itself, and not a configurable wrapper of another awaitable (i.e. the Task). Another difference is that Task.Yield does continue on the captured context.

Task.Run is different then both as it just takes a delegate and runs it on the ThreadPool, you can use it with ConfigureAwait(false) or without.

Task.Yield should be used to force an asynchronous point, not as a replacement to Task.Run. When await is reached in an async method it checks whether the task (or other awaitable) already completed and if so it continues on synchronously. Task.Yield prevents that from happening so it's useful for testing.

Another usage is in UI methods, where you don't want to be hogging the single UI thread, you insert an asynchronous point and the rest is scheduled to be executed in a later time.

like image 137
i3arnon Avatar answered Nov 16 '22 19:11

i3arnon


Task.Yield continues on the current synchronization context or on the current TaskScheduler if one is present. Task.Run does not do that. It always uses the thread-pool.

For example Task.Yield would stay on the UI thread.

Avoid Task.Yield. It's semantics are less clear. The linked answer is a code smell.

like image 7
usr Avatar answered Nov 16 '22 21:11

usr