How to cancel parallel loops in .NET C# z


CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

Task.Factory.StartNew(() =>
{
Thread.Sleep(5000);
cancellationTokenSource.Cancel();
Console.WriteLine("Token cancelled");
});

ParallelOptions parallelLoopOptions =
new ParallelOptions()
{
CancellationToken = cancellationTokenSource.Token
};

try
{
Parallel.For(0, Int64.MaxValue, parallelLoopOptions, index =>
{
double result = Math.Pow(index, 3);
Console.WriteLine("Index {0}, result {1}", index, result);
Thread.Sleep(100);
});
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancellation exception caught!");
}

How to cancel parallel loops in .NET C# z

时间: 2024-10-24 03:49:40

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

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 index: this is exclusive, so it won't be processed in the loop An Action of int which represents the method that should be executed in each loop, where in

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> dataList = new List<string> { "this", "is", "random", "sentence", "hello", "goodbye" }

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

List<int> integers = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Parallel.ForEach(integers, (int item, ParallelLoopState state) => { if (item > 5) { Console.WriteLine("Higher than 5: {0}, exiting loop.", item); state.Stop

Parallel stepped for loops in .NET C# z

for (int i = 0; i < 20; i += 2) public IEnumerable<int> SteppedIntegerList(int startIndex, int endEndex, int stepSize) { for (int i = startIndex; i < endEndex; i += stepSize) { yield return i; } } Parallel.ForEach(SteppedIntegerList(0, 20, 2),

Optimize Managed Code For Multi-Core Machines

Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This article discusses: Task Parallel Library Parallel.For versus ThreadPool Static Work Distribution Futures This article uses the following technologies: P

.NET并行编程1 -

设计模式--.net并行编程,清华大学出版的中译本. 相关资源地址主页面: http://parallelpatterns.codeplex.com/ 代码下载: http://parallelpatterns.codeplex.com/releases/view/50473 书籍在线地址: https://msdn.microsoft.com/en-us/library/ff963553.aspx 使用并行编程的一些示例: https://code.msdn.microsoft.com/Par

.NET 并行编程

本文内容 并行编程 数据并行 最近,对多线程编程,并行编程,异步编程,这三个概念有点晕了,怎么突然觉得,自己有点不明白这三者之间有什么联系和区别了呢? 因此,回顾了一下个人经历,屡屡思路~我刚接触计算机时,还是学校的 DOS 和 win 3.x,之后,学校换了 Windows 95,再之后,我有自己的台式机--但是无论如何,那时的电脑 CPU 都是单核的,即便采用多线程,无论看上多么像"同时"执行的,本质上还是顺序的,因为代码段是独占 CPU 的:之后,我把台式机卖了,买了个笔记本电脑

Plinq-Parallel.ForEach for 性能提升

https://msdn.microsoft.com/zh-cn/library/dd460720.aspx 本示例显示如何使用 Parallel.ForEach 循环对任何 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T> 数据源启用数据并行. 注意 本文档使用 lambda 表达式在 PLINQ 中定义委托.  如果您不熟悉 C# 或 Visual Basic 中的 lambda 表达式,请

(转载)基于Bash命令行的百度云上传下载工具

原文链接:http://hi.baidu.com/meoow/item/aef5814bbd5be3e1bcf451e9 这是我根据百度云PCS的API写的一个基于bash的命令行工具, 使用了curl, awk, sed, xxd, bash,没有使用任何其他的语言解释器做辅助 目前,上传下载,文件列表,文件信息,创建文件夹,删除文件都没问题 复制和移动总是返回参数错误,实在找不出来哪里跟API不一致了,暂时放弃. 另外离线下载相关的API也都添加了,但是因为现在百度云不能申请,我是用的是by