CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

Task.Factory.StartNew(() =>
{
Thread.Sleep();
cancellationTokenSource.Cancel();
Console.WriteLine("Token cancelled");
}); ParallelOptions parallelLoopOptions =
new ParallelOptions()
{
CancellationToken = cancellationTokenSource.Token
}; try
{
Parallel.For(, Int64.MaxValue, parallelLoopOptions, index =>
{
double result = Math.Pow(index, );
Console.WriteLine("Index {0}, result {1}", index, result);
Thread.Sleep();
});
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancellation exception caught!");
}

How to cancel parallel loops in .NET C# z的更多相关文章

  1. Parallel for loops in .NET C# z

    The start index: this is inclusive, i.e. this will be the first index value in the loop The end inde ...

  2. Parallel for-each loops in .NET C# z

    An IEnumerable object An Action of T which is used to process each item in the list List<string&g ...

  3. Parallel stepped for loops in .NET C# z

    ; i < ; i += ) public IEnumerable<int> SteppedIntegerList(int startIndex, int endEndex, int ...

  4. Breaking parallel loops in .NET C# using the Stop method z

    List<, , , , , , , , , }; Parallel.ForEach(integers, (int item, ParallelLoopState state) => { ...

  5. Task Cancellation: Parallel Programming

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

  6. C++11: Multi-core Programming – PPL Parallel Aggregation Explained

    https://katyscode.wordpress.com/2013/08/17/c11-multi-core-programming-ppl-parallel-aggregation-expla ...

  7. .NET并行编程1 - 并行模式

    设计模式——.net并行编程,清华大学出版的中译本. 相关资源地址主页面: http://parallelpatterns.codeplex.com/ 代码下载: http://parallelpat ...

  8. Optimize Managed Code For Multi-Core Machines

    Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This ar ...

  9. 7.OpenACC

    OpenACC: openacc 可以用于fortran, c 和 c++程序,可以运行在CPU或者GPU设备. openacc的代码就是在原有的C语言基础上进行修改,通过添加:compiler di ...

随机推荐

  1. 【bzoj3110】[Zjoi2013]K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置 ...

  2. R语言编程艺术# 数据类型向量(vector)

    R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用t ...

  3. 论文阅读(2014-2)----The YouTube Video Recommendation System

    这是谷歌youtube在2010的一篇文章,估计现在的思路有很多升级了,但是里面的知识点还是很不错的.主要讲youtube的个性化推荐思路.下面根据论文的结构我把我理解的思路整理如下,如果有问题,欢迎 ...

  4. shell复习笔记----用户管理

    $ who    可以知道系统上有多少登陆 $who |wc -l 计算用户个数 注意:|是管道符号,可以在两个程序之间建立管道(pipeline):who 的输出,成了 wc 的输入, wc 所列出 ...

  5. nginx流量带宽等请求状态统计( ngx_req_status)

    介绍 ngx_req_status用来展示nginx请求状态信息,类似于apache的status,nginx自带的模块只能显示连接数等等信息,我们并不能知道到底有哪些请求.以及各url域名所消耗的带 ...

  6. Java连接redis的使用示例

    在多线程下使用Jedis 在不同的线程中使用相同的Jedis实例会发生奇怪的错误.但是创建太多的实现也不好因为这意味着会建立很多sokcet连接,也会导致奇怪的错误发生.单一Jedis实例不是线程安全 ...

  7. 现代浏览器原生js获取id号方法

    <div id="tests" class="a b c" style="color:#f00">123</div> ...

  8. 在js中获取easyui datagrid的数据

    可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法,但是怎么用了,没说. 最后这样写才搞定 var a ...

  9. 利用钩子函数来捕捉键盘响应的windows应用程序

    一:引言: 你也许一直对金山词霸的屏幕抓词的实现原理感到困惑,你也许希望将你的键盘,鼠标的活动适时的记录下来,甚至你想知道木马在windows操作系统是怎样进行木马dll的加载的…..其实这些都是用到 ...

  10. import,reload,__import__在python中的区别

    import,reload,__import__在python中的区别 http://blog.csdn.net/five3/article/details/7762870 import作用:导入/引 ...