1

以前に、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))
4

1 に答える 1

1

Catchによってスローされた例外をキャッチしません。ブロックしないためTask、例外がスローされたときに はアクティブではなくなります。StartNew()Catch

終了後に何かをしたい場合Taskは、 のオーバーロードの 1 つを使用できますContinueWith()。それらの一部では、継続をいつ実行するかを正確に指定できます。タスクが正常に終了した場合、失敗またはキャンセルされた場合 (またはそれらの組み合わせ) のみです。

于 2012-05-16T16:52:02.467 に答える