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. poi导出到excel步骤分析

    在没用过poi之前感觉poi是很高大上的样子, 项目中用了发现poi的代码重复性很高类似于jdbc的模板代码, 项目中如果大量使用最好封装起来; 总结一下归结为6步 1 打开或新创建一个工作薄(使用H ...

  2. iOS百度地图路径规划和POI检索详细总结-b

    路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj.png 如上图所示,在infoplist里 ...

  3. Eclipse升级到4.4.2后界面主题更改

    在win8.1电脑上一直很喜欢eclipse luna sr1a(4.4.1)版本的界面好像是软件自动设置的. 这几天更新到eclipse luna sr2(4.4.2)版本后发现界面大变,怎么也找不 ...

  4. java第一课:环境、变量、数据类型

    一.java编程注意事项1.java区分大小写2.每条语句结尾有分号3.上下级代码注意缩进4.大括号要成对出现5.标点符号要用英文半角(半角全角区别)二.eclipse1.eclipse是自编译及时编 ...

  5. 【贪心】bzoj 3709:[PA2014]Bohater

    3709: [PA2014]Bohater Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 653  Solved:  ...

  6. [转载]jQuery.lazyload详解 - 图片延时加载

    jQuery实现图片延迟加载,不知道是否可以节省带宽呢?有人知道吗? 这究竟只是一个视觉特效还是真的能延迟加载减少服务器的请求呢? <script type="text/javascr ...

  7. HZNU1015: 矩阵排序

    http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1015 题意:把矩阵每一行都排序. (以前觉得很难的题目回头看看原来如此简单 ][]; ; i&l ...

  8. ACM题集以及各种总结大全!

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  9. Cloud Insight 和 BearyChat 第一次合体,好紧张!

    说到 ChatOps 我们可能立刻想到是 Slack(啥?没听过?哦!),但是由于国内网络和语言的问题你可能无法拥有很好的体验了.那就把目光转回国内吧,国内的话就不得不提到 BearyChat 等 C ...

  10. define中的:#,##,#@

    [define中的:#,##,#@] #define Conn(x,y) x##y #define ToChar(x) #@x #define ToString(x) #x (2)x##y表示什么?表 ...