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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With