The Test_Click
below is a simplified version of code which runs on a UI thread (with WindowsFormsSynchronizationContext):
void Test_Click(object sender, EventArgs e)
{
var task = DoNavigationAsync();
task.ContinueWith((t) =>
{
MessageBox.Show("Navigation done!");
}, TaskScheduler.FromCurrentSynchronizationContext());
}
Should I explicitly specify TaskScheduler.FromCurrentSynchronizationContext()
to make sure the continuation action will be executed on the same UI thread? Or does ContinueWith
capture the execution context automatically (thus, making TaskScheduler
argument redundant in this case)?
I assume it doesn't do it by default (unlike await
), but so far I could not find an online resource to confirm this.