×

parallel.foreach

parallel.foreach(parallelforeach更新数据库无效)

admin admin 发表于2023-04-08 05:48:11 浏览75 评论0

抢沙发发表评论

本文目录一览:

简述parallel帮助器类有哪些

Parallel类是对线程的一个抽象。该类位于System.Threading.Tasks名称空间中,提供了数据和任务并行性。

Paraller类定义了数据并行地For和ForEach的静态方法,以及任务并行的Invoke的静态方法。Parallel.For()和Parallel.ForEach()方法在每次迭代中调用相同的代码,Paraller.Invoke()允许调用不同的方法。

请问如何:编写简单的 Parallel.ForEach 循环

示例 Visual Basic ' How to: Write a Simple Parallel.ForEach Loop ' IMPORTANT!!!: Add reference to System.Drawing.dll Imports System.Threading Imports System.Threading.Tasks Imports System.Drawing Module ForEachDemo Sub Main() ' A simple source for demonstration purposes. Modify this path as necessary. Dim files AsString() = System.IO.Directory.GetFiles("C:\Users\Public\Pictures\Sample Pictures", "*.jpg") Dim newDir AsString = "C:\Users\Public\Pictures\Sample Pictures\Modified" System.IO.Directory.CreateDirectory(newDir) ' Method signature: Parallel.ForEach(IEnumerableTSource source, ActionTSource body) ' Be sure to add a reference to System.Drawing.dll. Parallel.ForEach(files, Sub(currentFile) ' The more computational work you do here, the greater ' the speedup compared to a sequential foreach loop. Dim filename AsString = System.IO.Path.GetFileName(currentFile) Dim bitmap AsNew System.Drawing.Bitmap(currentFile) bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone) bitmap.Save(System.IO.Path.Combine(newDir, filename)) ' Peek behind the scenes to see how work is parallelized. ' But be aware: Thread contention for the Console slows down parallel loops!!! Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId) 'close lambda expression and method invocation EndSub) ' Keep the console window open in debug mode. Console.WriteLine("Processing complete. Press any key to exit.") Console.ReadKey() EndSubEndModule C# namespace ForEachDemo { using System; using System.Drawing; // requires system.Drawing.dll using System.IO; using System.Threading; using System.Threading.Tasks; class SimpleForEach { staticvoid Main() { // A simple source for demonstration purposes. Modify this path as necessary.string[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; System.IO.Directory.CreateDirectory(newDir); // Method signature: Parallel.ForEach(IEnumerableTSource source, ActionTSource body) Parallel.ForEach(files, currentFile = { // The more computational work you do here, the greater // the speedup compared to a sequential foreach loop.string filename = System.IO.Path.GetFileName(currentFile); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile); bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); bitmap.Save(System.IO.Path.Combine(newDir, filename)); // Peek behind the scenes to see how work is parallelized.// But be aware: Thread contention for the Console slows down parallel loops!!! Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId); } //close lambda expression ); //close method invocation // Keep the console window open in debug mode. Console.WriteLine("Processing complete. Press any key to exit."); Console.ReadKey(); } } } ForEach 循环的工作方式类似于 For 循环。根据系统环境,对源集合进行分区,并在多个线程上计划工作。系统中的处理器越多,并行方法的运行速度越快。对于某些源集合,顺序循环可能更快,具体取决于源的大小和正在执行的工作类型。有关性能的更多信息,请参见数据并行和任务并行中的潜在缺陷有关并行循环的更多信息,请参见如何:编写简单的 Parallel.For 循环若要将 ForEach 用于非泛型集合,可以使用 Cast(Of (TResult)) 扩展方法将集合转换为泛型集合,如下面的示例所示: Visual Basic Parallel.ForEach(nonGenericCollection.Cast(OfObject), _ Sub(currentElement) ' ... work with currentElement EndSub) C# Parallel.ForEach(nonGenericCollection.Castobject(), currentElement = { }); 还可以使用并行 LINQ (PLINQ) 来并行处理 IEnumerable(Of (T)) 数据源。PLINQ 使您可以使用声明性的查询语法表达循环行为。-parallel.foreach

c#双核多线程并行计算 Parallel是不是最优方案

1 Parallel.ForEach 会用当前线程一起算 所以要注意最好放在 Task.Start里面 将返回await 不然有可能造成死锁类问题。

2 就算你确定是cpu 密集的 也要注意并行度,最好不要超过你的核心数n多倍。

对于cpu密集的操作控制并行度可以用 Parallel 的参数。

如果其中有IO操作 最好用 TPL.Dataflow 库中的ActionBloackT等来控制异步操作并行度

何时使用 Parallel.ForEach,何时使用 PLINQ

(Task Parallel Library)和PLINQ(Parallel LINQ),这....Parallel 静态类提供了三个重要的方法For、Foreach...以StopWatch进行精确时间测试,并行环境下的执行时间为...-parallel.foreach

C#中原来串行代码为 foreach (DataRow row in ResultTable.Rows)请问如何改为Parallel.ForEach并行写法?

应该是

Parallel.ForEach(

ResultTable.Rows,

(row) ={

//处理语句,比如

Console.WriteLine(row.ToString());

}

);

c#foreach 是异步的吗

foreach自然是同步的

异步你可以使用

Parallel.Foreach来进行异步