This is a follow-up to this question.
Question: What would be a succinct way to express the following using async/await instead of .ContinueWith()?:
var task = Task.Run(() => LongRunningAndMightThrow());
m_cts = new CancellationTokenSource();
CancellationToken ct = m_cts.Token;
var uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task updateUITask = task.ContinueWith(t => UpdateUI(t), ct, TaskContinuationOptions.None, uiTaskScheduler);
I'm mainly interested in the case of a UI SynchronizationContext (e.g. for Winforms)
Note with this that the behavior has all the following desired behaviors:
When the
CancellationTokenis cancelled, theupdateUITaskends up cancelled as soon as possible (i.e. theLongRunningAndMightThrowwork may still be going on for quite some time).The
ctCancellationToken gets checked for cancellation on the UI thread prior to running the UpdateUI lambda (see this answer).The
updateUITaskwill end up cancelled in some cases where thetaskcompleted or faulted (since thectCancellationToken is checked on the UI thread before executing the UpdateUI lambda.There is no break in flow between the check of the
CancellationTokenon the UI thread and the running of theUpdateUIlambda. That is, if theCancellationTokenSourceis only ever cancelled on the UI thread, then there is no race condition between the checking of theCancellationTokenand the running of theUpdateUIlambda--nothing could trigger theCancellationTokenin between those two events because the UI thread is not relinquished in between those two events.
Discussion:
One of my main goals in moving this to async/await is to get the
UpdateUIwork out of a lambda (for ease of readability/debuggability).#1 above can be addressed by Stephen Toub's
WithCancellationtask extension method. (which you can feel free to use in the answers).The other requirements seemed difficult to encapsulate into a helper method without passing
UpdateUIas a lambda since I cannot have a break (i.e. anawait) between the checking of theCancellationTokenand the executing ofUpdateUI(because I assume I cannot rely on the implementation detail thatawaitusesExecuteSynchronouslyas mentioned here. This is where it seems that having the mythicalTaskextension method.ConfigureAwait(CancellationToken)that Stephen talks about would be very useful.I've posted the best answer I have right now, but I'm hoping that someone will come up with something better.
Sample Winforms Application demonstrating the usage:
public partial class Form1 : Form
{
CancellationTokenSource m_cts = new CancellationTokenSource();
private void Form1_Load(object sender, EventArgs e)
{
cancelBtn.Enabled = false;
}
private void cancelBtn_Click(object sender, EventArgs e)
{
m_cts.Cancel();
cancelBtn.Enabled = false;
doWorkBtn.Enabled = true;
}
private Task DoWorkAsync()
{
cancelBtn.Enabled = true;
doWorkBtn.Enabled = false;
var task = Task.Run(() => LongRunningAndMightThrow());
m_cts = new CancellationTokenSource();
CancellationToken ct = m_cts.Token;
var uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task updateUITask = task.ContinueWith(t => UpdateUI(t), ct, TaskContinuationOptions.None, uiTaskScheduler);
return updateUITask;
}
private async void doWorkBtn_Click(object sender, EventArgs e)
{
try
{
await DoWorkAsync();
MessageBox.Show("Completed");
}
catch (OperationCanceledException)
{
MessageBox.Show("Cancelled");
}
catch
{
MessageBox.Show("Faulted");
}
}
private void UpdateUI(Task<bool> t)
{
// We *only* get here when the cancel button was *not* clicked.
cancelBtn.Enabled = false;
doWorkBtn.Enabled = true;
// Update the UI based on the results of the task (completed/failed)
// ...
}
private bool LongRunningAndMightThrow()
{
// Might throw, might complete
// ...
return true;
}
}
Stephen Toub's WithCancellation extension method:
public static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using(cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
if (task != await Task.WhenAny(task, tcs.Task))
throw new OperationCanceledException(cancellationToken);
return await task;
}
Related Links: