HOW将foreach循环转换为并行线程 [英] H0w to convert foreach loop into parallel threads

查看:79
本文介绍了HOW将foreach循环转换为并行线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我正在使用foreach循环来执行我的任务。在循环中,我调用一种方法,在将内容添加到数据库后将文件从源传输到目标。如何将foreach循环更改为线程,即因为许多文件存在相同数量的线程将并行运行。





I am using a foreach loop to perform my task. In the loop I am calling a single method to transfer a file from source to destination after the content is being added to the database. How can I change the foreach loop to threads i.e as many of the files are there the same number of threads will be running parallel.

foreach (string strFilePath in strFilePaths)
                   {
                       ProcessXML(strFilePath);
                   }





如有任何帮助,我们将不胜感激。



谢谢



Any help will be appreciated.

Thanks

推荐答案

您可以获得以下代码帮助........



进口系统.Threading

Imports System.Threading.Tasks

Imports System.Drawing



Module ForEachDemo



Sub Main()

'用于演示目的的简单来源。根据需要修改此路径。

Dim files As String()= System.IO.Directory.GetFiles(C:\Users\Public\Pictures\Sample Pictures,* .jpg)

Dim newDir As String =C:\ Users \Public \Pictures \Sample Pictures \Modified

System.IO.Directory.CreateDirectory(newDir)



'方法签名:Parallel.ForEach(IEnumerable< tsource> source,Action< tsource> body)

'请务必添加对System.Drawing.dll。

Parallel.ForEach(文件,Sub(currentFile)

'你在这里做的计算工作越多,

'与顺序foreach循环相比的加速。

Dim filename As String = System.IO.Path.GetFileName(currentFile)

Dim bitmap As New System.Drawing。位图的处理ap(currentFile)



bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone)

bitmap.Save(System.IO.Path。组合(newDir,文件名))



'在幕后窥视工作是如何并行化的。

'但请注意:控制台的线程争用会降低并行循环的速度!!!



Console.WriteLine(处理{0} on thread {1},filename,Thread.CurrentThread.ManagedThreadId)

'关闭lambda表达式和方法调用

结束Sub)





'在调试模式下保持控制台窗口打开。

Console.WriteLine(处理完成。按任意键退出。)< br $>
Console.ReadKey()

结束子

结束模块



A ForEach循环就像For循环一样。源集合已分区,并且基于系统环境在多个线程上调度工作。系统上的处理器越多,并行方法运行得越快。对于某些源集合,顺序循环可能更快,具体取决于源的大小和正在执行的工作类型。有关性能的更多信息
you can get help following code........

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 As String() = System.IO.Directory.GetFiles("C:\Users\Public\Pictures\Sample Pictures", "*.jpg")
Dim newDir As String = "C:\Users\Public\Pictures\Sample Pictures\Modified"
System.IO.Directory.CreateDirectory(newDir)

' Method signature: Parallel.ForEach(IEnumerable<tsource> source, Action<tsource> 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 As String = System.IO.Path.GetFileName(currentFile)
Dim bitmap As 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 and method invocation
End Sub)


' Keep the console window open in debug mode.
Console.WriteLine("Processing complete. Press any key to exit.")
Console.ReadKey()
End Sub
End Module

A ForEach loop works like a For loop. The source collection is partitioned and the work is scheduled on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs. For some source collections, a sequential loop may be faster, depending on the size of the source, and the kind of work being performed. For more information about performance


这篇关于HOW将foreach循环转换为并行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆