我收到了一些很好的意见之前在parallel.foreach VS Task.Factory.StartNew。 我已经实现双方并已惊讶两者的效率。 我用下面的链接http://msdn.microsoft.com/en-us/library/dd997415.aspx尝试和了解例外,并纳入如果任务因任何原因停止该程序会检测它,它得到通知。 是否有任何明确的方式来做到这一点无需等待()或为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))