以前に、parallel.foreach と Task.Factory.StartNew について良いアドバイスをいくつか受け取りました。私は両方を実装しましたが、両方の効率に驚いています。次のリンクhttp://msdn.microsoft.com/en-us/library/dd997415.aspxを使用して、例外を理解しようとし、プログラムが何らかの理由でタスクを停止した場合に通知されるように組み込みました。インターフェイスと同時に実行されている他のタスクを結び付けるwait()またはwaitallなしでこれを行う決定的な方法はありますか?
Try
pcounter += 1
Dim factory As Task = Task.Factory.StartNew(AddressOf FileParser.Module1.Main)
If factory.IsCompleted Then
appLogs.constructLog("GT19 Task Completed", True, True)
End If
Button1.Text = pcounter.ToString & " processes started"
If Not TextBox1.Text = "" Then
Module1.inputfolder = TextBox1.Text
End If
Catch ae As AggregateException
For Each ex In ae.InnerExceptions
appLogs.constructLog(ex.Message.ToString & " ", True, True)
Next
Button1.Text = "ERROR RECEIVED"
Catch ex As Exception
If ex.Message.Contains("cannot access") Then
appLogs.constructLog(ex.Message.ToString & " ", True, True)
End If
appLogs.constructLog(ex.Message.ToString & " ", True, True)
appLogs.constructLog(" Cancelling process ", True, True)
Finally
Module1.ctsources.Cancel()
End Try
今、ボタン呼び出しと関数でテストしてみました:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Module1.ctsources.Cancel()
Button2.Text = "process stopped"
FileParser.Module1.Main 内
If ct.IsCancellationRequested Then
sendErrorEmail()
Exit Sub
End If
しかし、プロセスが停止したという確認が得られません。また、parallel.foreach を使用する場合
Dim po As New ParallelOptions
po.MaxDegreeOfParallelism = 3
Parallel.ForEach(fileLists, po, Sub(page) processFile(page))