close

Imports System.Threading
Imports System.Threading.Tasks
Imports System.Drawing
'要專案 參考 System.Drawing.dll

'------------
'平行 foreach 使用方法
'------------

Module Module1 'VB2010 主控台程式

Sub Main()

' A simple source for demonstration purposes. Modify this path as necessary.
Dim files As String() = System.IO.Directory.GetFiles("C:\11", "*.jpg") '從C:\11 資料夾 找副檔名.jpg 的圖
Dim newDir As String = "C:\JK" '在 C:\ 產生JK目錄
System.IO.Directory.CreateDirectory(newDir)
' Method signature: Parallel.ForEach(IEnumerable source, Action body)
' Be sure to add a reference to System.Drawing.dll.
Parallel.ForEach(files, Sub(mm)
'上一行不能有註解 會破壞語法


' 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(mm)
Dim bitmap As New System.Drawing.Bitmap(mm)

bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone) '抓一張圖來旋轉180度
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

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 prague12 的頭像
    prague12

    prague12

    prague12 發表在 痞客邦 留言(0) 人氣()