b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

编写简单的 Parallel.ForEach 循环

电脑杂谈  发布时间:2016-04-25 08:00:55  来源:网络整理

你是否正在寻找关于parallel.foreach的内容?让我把最高级的东西奉献给你:

代码片段(2) [全屏查看所有代码]

1.[代码][C#]代码

using System; using System.Drawing; // requires system.Drawing.dll using System.IO; using System.Threading; using System.Threading.Tasks; class SimpleForEach { static void Main() { // A simple source for demonstration purposes. Modify this path as necessary. string[] files = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; Directory.CreateDirectory(newDir); // Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body) parallel.foreach(files, currentFile => { // The more computational work you do here, the greater // the speedup compared to a sequential foreach loop. string filename = Path.GetFileName(currentFile); Bitmap bitmap = new Bitmap(currentFile); bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); bitmap.Save(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(); } }

2.[代码]要将 ForEach 用于非泛型集合,可以使用 Cast

以上就是关于parallel.foreach的全部内容,相信你一定会非常满意,。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-2841-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...