码迷,mamicode.com
首页 > 其他好文 > 详细

防止两提交同一异步任务

时间:2015-07-24 22:24:48      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

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)
{
    ...
}

 

防止两提交同一异步任务

标签:

原文地址:http://www.cnblogs.com/zeroone/p/4674715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!