If I need to postpone code execution until after a future iteration of the UI thread message loop, I could do so something like this:
await Task.Factory.StartNew(
() => {
MessageBox.Show("Hello!");
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
This would be similar to await Task.Yield(); MessageBox.Show("Hello!");
, besides I'd have an option to cancel the task if I wanted to.
In case with the default synchronization context, I could similarly use await Task.Run
to continue on a pool thread.
In fact, I like Task.Factory.StartNew
and Task.Run
more than Task.Yield
, because they both explicitly define the scope for the continuation code.
So, in what situations await Task.Yield()
is actually useful?