防止两提交同一异步任务
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) { ... }
相关文章推荐
- libpcap函数详解
- 《Unix网络编程卷1-套接字联网API》第一个例子编译
- 平衡括号问题
- hd1002
- 十种NoSQL数据库以及对比
- JavaScript学习链接
- 制品的注文设定
- Java Type VS Hibernate Type
- 开发Google Material Design风格的WPF程序
- hdu 1281 棋盘游戏(二分匹配)
- 基于xml类型的压缩数据流的android获取天气的方法
- android之数据存取
- ngx_rbtree_t红黑树
- hadoop开发:eclipse运行wordcount实例
- 利用getViewTreeObserver().addOnGlobalLayoutListener()获得一个视图的高度
- hdoj1002 A + B Problem II 初学大数相加
- 构造函数为什么不能是虚函数
- Android—开发自学历程(1)-Intent-第二个Activity
- 2015 HUAS Summer Training#2~D
- HDU5301