zoukankan      html  css  js  c++  java
  • How to:Aborting a long running task in TPL

    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.

  • 相关阅读:
    alloffthelights使用方法
    tweenMax学习笔记
    移动端获取手机摄像头和相册
    livereload使用方法
    Bower使用笔记
    github 远程仓库
    git for windows 本地仓库
    python 对文件操作
    Python 装饰器
    JavaScript 做的网页版扫雷小游戏
  • 原文地址:https://www.cnblogs.com/rock_chen/p/3424426.html
Copyright © 2011-2022 走看看