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:

    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.

How to:Aborting a long running task in TPL的更多相关文章

  1. get running task , process and service

    public class MyActivityManager extends ExpandableListActivity { private static final String NAME = & ...

  2. asp.net web api long running task

    http://stackoverflow.com/questions/17577016/long-running-task-in-webapi http://blog.stephencleary.co ...

  3. TaskTracker任务初始化及启动task源码级分析

    在监听器初始化Job.JobTracker相应TaskTracker心跳.调度器分配task源码级分析中我们分析的Tasktracker发送心跳的机制,这一节我们分析TaskTracker接受JobT ...

  4. C#并行编程-Task

    菜鸟学习并行编程,参考<C#并行编程高级教程.PDF>,如有错误,欢迎指正. 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 ...

  5. 【转】【C#】【Thread】【Task】多线程

    多线程 多线程在4.0中被简化了很多,仅仅只需要用到System.Threading.Tasks.::.Task类,下面就来详细介绍下Task类的使用. 一.简单使用 开启一个线程,执行循环方法,返回 ...

  6. 【Hadoop代码笔记】Hadoop作业提交之TaskTracker获取Task

    一.概要描述 在上上一篇博文和上一篇博文中分别描述了jobTracker和其服务(功能)模块初始化完成后,接收JobClient提交的作业,并进行初始化.本文着重描述,JobTracker如何选择作业 ...

  7. Hadoop作业提交之TaskTracker获取Task

    [Hadoop代码笔记]Hadoop作业提交之TaskTracker获取Task 一.概要描述 在上上一篇博文和上一篇博文中分别描述了jobTracker和其服务(功能)模块初始化完成后,接收JobC ...

  8. Task Cancellation: Parallel Programming

    http://beyondrelational.com/modules/2/blogs/79/posts/11524/task-cancellation-parallel-programming-ii ...

  9. Linux Hung Task分析

    关键词:khungtaskd.TASK_UNINTERRUPTIBLE.nvcsw.nivcsw.last_switch_count等等. 经常会遇到内核打印“INFO: task xxx:xxx b ...

随机推荐

  1. keil中结构体跨文件调用

    在a.h中: 定义了, struct ABC{ short a; short b; ```}; 在a.c中(#include "a.h"): 声明了, struct ABC stc ...

  2. 直接用SQL语句把DBF导入SQLServer

    直接用SQL语句把DBF导入SQLServer   在SQLServer中执行 SELECT * into bmk FROM OpenDataSource( ’Microsoft.Jet.OLEDB. ...

  3. rails 网站跨域

    7down voteaccepted gem install rack-cors Or in your Gemfile: gem 'rack-cors', :require => 'rack/c ...

  4. Spring定时器Quartz的使用

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等,定时更新某某操作……. 我们可以使用java.util.Timer结合java.util.TimerT ...

  5. vertex shader(3)

    之前我们学习了如何声明顶点着色器.如何设置常量寄存器中的常量.接下来我们学习如何写和编译一个顶点着色器程序. 在我们编译一个顶点着色器之前,首先需要写一个. 有17种不同的指令(instruction ...

  6. a+b_1

    题目截图: 思路: 直接输出即可. 代码如下: /* a+b */ #include <stdio.h> #include <string.h> #include <ma ...

  7. 【转】c++虚函数实现原理

    原文链接:https://blog.csdn.net/neiloid/article/details/6934135 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指 ...

  8. Linux环境下安装myeclipse+破解

    1.下载myeclipse安装包,下载myeclipse破解文件. 2.修改myeclipse-pro-2014-GA-offline-installer-linux.run的权限 sudo chmo ...

  9. glTexGen

    [glTexGen] Rather than having to explicitly provide a texture coordinate for each vertex, we can use ...

  10. 【POJ1509】Glass Beads 【后缀自动机】

    题意 给出一个字符串,求它的最小表示法. 分析 这个题当然可以用最小表示法做啦!但是我是为了学后缀自动机鸭! 我们把这个字符串长度乘二,然后建SAM,然后在SAM上每次跑最小的那个字母,找出长度为n的 ...