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. EXTJS 4.2 资料 控件之tabpanel 静态生成tabpanel

    //**************页面主体开始***************** var tabpanel = Ext.createWidget('tabpanel', { activeTab: 0, ...

  2. ISoft(开源)专用下载器

    继 两年的坚持,最后还是决定将ISoft开源 之后,今天再共享一款ISoft专用下载器小工具.这款工具是一年前开发的,也是一直闲置着没去扩展更新.当时开发出来就是仿穿越火线专用下载器的样式来做的,现在 ...

  3. iOS通过http post上传图片 (转)

    转载自:http://www.cocoachina.com/bbs/read.php?tid=89985 由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他 ...

  4. 【BZOJ 1877】 [SDOI2009]晨跑

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...

  5. iOS视频压缩存储至本地并上传至服务器-b

    最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...

  6. C#列表顺序替换思想

    /// <summary> /// 显示列顺序 /// </summary> /// <param name="list"></param ...

  7. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  8. Matlab计算矩阵间距离

    夜深人静时分,宿舍就我自己,只有蚊子陪伴着我,我慢慢码下这段文字............ 感觉知识结构不完善:上学期看论文,发现类间离散度矩阵和类内离散度矩阵,然后百度,找不到,现在学模式识别,见了, ...

  9. 解决VS如何同时打开两个工程(xp和win7)

    http://www.360doc.com/content/11/1020/00/7891073_157586269.shtml

  10. Good Bye 2015 A

    Problem A:http://codeforces.com/problemset/problem/611/A A. New Year and Days 题意:某人要在2016年收集糖果,有两种不同 ...