How to:Aborting a long running task in TPL
2013-11-14 22:48
309 查看
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d0bcb415-fb1e-42e4-90f8-c43a088537fb/aborting-a-long-running-task-in-tpl?forum=parallelextensions
I hesitate to show this :), but if you really did want to use aborts, you could do something like:
Then, if the token receives a cancellation request, it'll translate that into an abort on the thread running the compute-bound work. Of course, it'd be better if the "crunchy" operation were written to monitor the token, checking its IsCancellationRequest property or calling ThrowIfCancellationRequested every once in a while. This is what methods like Parallel.For and PLINQ do, and it provides for a nice cooperative cancellation mechanism whereby the caller can still be terminated in a timely fashion, but it can do so at a particular point when it's safe.
I hope that helps.
I hesitate to show this :), but if you really did want to use aborts, you could do something like:
int Foo(CancellationToken token) { Thread t = Thread.CurrentThread; using (token.Register(t.Abort)) { // compute-bound work here } }
Then, if the token receives a cancellation request, it'll translate that into an abort on the thread running the compute-bound work. Of course, it'd be better if the "crunchy" operation were written to monitor the token, checking its IsCancellationRequest property or calling ThrowIfCancellationRequested every once in a while. This is what methods like Parallel.For and PLINQ do, and it provides for a nice cooperative cancellation mechanism whereby the caller can still be terminated in a timely fashion, but it can do so at a particular point when it's safe.
I hope that helps.
相关文章推荐
- php中的单引号和双引号的区别转
- XSS攻击的过滤
- php页面请求原理
- PHP报错Bus error解决方法
- Zend Studio下的PHP代码调试
- 【总结】关于MediaPlayer中的getCurrentPosition()和seekTo(int)的总结
- php基础系列:从用户登录处理程序学习mysql扩展基本操作
- IFormatProvider接口
- h264 rtp打包
- php字符串简介
- PHP编程遇到的小问题
- PHP扩展官方搜索地址
- Pure-FTPd
- FTP 服务
- cacti插件学习手札--php-Weathermap
- EditPlus正则表达式
- php 解析 视频 信息 封面 标题 图片 支持 优酷, 土豆 酷6 56 新浪 qq播客 乐视 乐视
- 用FileZilla Server开FTP
- 在PHP语言中使用JSON
- vsto插件在64位Office的一个异常---算术运算溢出(IntPtr转int)