I need a re-entry task, and implementing similar code to https://blogs.msdn.microsoft.com/lucian/2014/03/03/async-re-entrancy-and-the-patterns-to-deal-with-it/ (pattern 5)
but I'm wondering if the CancellationTokenSource dispose is not missing. My implementation is adding it in .ContinueWith
private Task _fooAsyncTask;
private CancellationTokenSource _fooAsyncCancellation;
public async Task Button1Click()
{
// Assume we're being called on UI thread... if not, the two assignments must be made atomic.
// Note: we factor out "FooHelperAsync" to avoid an await between the two assignments.
// without an intervening await.
_fooAsyncCancellation?.Cancel();
_fooAsyncCancellation = new CancellationTokenSource();
_fooAsyncTask = FooHelperAsync(_fooAsyncCancellation.Token);
await _fooAsyncTask.ContinueWith(task =>
{
_fooAsyncCancellation.Dispose();
_fooAsyncCancellation = null;
});
}
private async Task FooHelperAsync(CancellationToken cancel)
{
try { if (_fooAsyncTask != null) await _fooAsyncTask; }
catch (OperationCanceledException) { }
cancel.ThrowIfCancellationRequested();
await FooAsync(cancel);
}
private async Task FooAsync(CancellationToken cancel)
{
//
}
Is that correct?
You should change your code to
fooAsyncCancellation
tonull
on the UI thread (in continuation it is not)Here the modified code