您的位置:首页 > 其它

防止两提交同一异步任务

2015-07-24 21:33 281 查看
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.
if  (FooAsyncCancellation !=  null ) FooAsyncCancellation.Cancel();
FooAsyncCancellation  =  new  CancellationTokenSource ();
FooAsyncTask  = FooHelperAsync(FooAsyncCancellation.Token);

await  FooAsyncTask;
}

Task  FooAsyncTask;
CancellationTokenSource  FooAsyncCancellation;

async  Task  FooHelperAsync( CancellationToken  cancel)
{
try  {  if  (FooAsyncTask !=  null )  await  FooAsyncTask; }
catch  ( OperationCanceledException ) { }
cancel.ThrowIfCancellationRequested();
await  FooAsync(cancel);
}

async  Task  FooAsync( CancellationToken  cancel)
{
...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: