0
我需要一個再入任務,並執行類似的代碼https://blogs.msdn.microsoft.com/lucian/2014/03/03/async-re-entrancy-and-the-patterns-to-deal-with-it/(模式5)重入模式5:將取消先前調用
但我想知道如果CancellationTokenSource處置是不缺的。我的實施將其添加進來。繼續使用
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)
{
//
}
這是正確的嗎?
沒有必要處置CTS。你可以處置它,但沒有背後的非託管資源,它是值得處置的。順便說一句,你並不需要一個延續,只是等待_fooAsyncTask –
第二次看後,不建議處置CTS - 如果你這樣做,你將獲得* _fooAsyncCancellation異常.Cancel(); * –
@SirRufo「它不建議處置CTS「 - 你有沒有參考。我只是尋找有關此事的一些「最佳實踐」 ...... – Fildor