1

以下は、UI スレッドによって正常に開始された、新しく作成されたスレッド コードです (UI コードはここにはありません)。

デバッグを 1 ステップ実行すると、このコードにたどり着きます。

実行中のワークフローに問題がなければ、コードは最後まで実行されます。しかし、ワークフローに問題がある場合 (問題のあるワークフローを使用してコードをテストしています)、このコードは以下のステートメントWorkflowExceptionで発生することをキャッチしません。wf.Run()

以下のワークフロー実行例外処理コードがあると思いますか?? 私が間違っていることを教えてもらえますか?ありがとう。

public void ThreadRun ()
    {  
            AutoResetEvent syncEvent = new AutoResetEvent(false);
            var wf = ActivityXamlServices.Load(fileInfo.FileName);
            // Create the WorkflowApplication using the desired
            // workflow definition.
            WorkflowApplication wfApp = new WorkflowApplication(wf);

            // Handle the desired lifecycle events.
            wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
            {
                syncEvent.Set();
            };

            try
            {
                // Start the workflow.
                wfApp.Run();
                // Wait for Completed to arrive and signal that
                // the workflow is complete.
                syncEvent.WaitOne();
            }
            catch (Exception exception)
            {

                wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
                {
                    if (e.CompletionState == ActivityInstanceState.Faulted)
                    {
                        Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
                        Console.WriteLine("Exception: {0}\n{1}",
                            e.TerminationException.GetType().FullName,
                            e.TerminationException.Message);
                    }
                    else if (e.CompletionState == ActivityInstanceState.Canceled)
                    {
                        Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
                    }
                    else
                    {
                        Console.WriteLine("Workflow {0} Completed.", e.InstanceId);

                        // Outputs can be retrieved from the Outputs dictionary,
                        // keyed by argument name.
                        // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
                    }
                };

                wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
                {
                    // Display the exception that caused the workflow
                    // to abort.
                    Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
                    Console.WriteLine("Exception: {0}\n{1}",
                        e.Reason.GetType().FullName,
                        e.Reason.Message);
                };

                wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
                {
                    // Perform any processing that should occur
                    // when a workflow goes idle. If the workflow can persist,
                    // both Idle and PersistableIdle are called in that order.
                    Console.WriteLine("Workflow {0} Idle.", e.InstanceId);
                };

                wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
                {
                    // Instruct the runtime to persist and unload the workflow.
                    // Choices are None, Persist, and Unload.
                    return PersistableIdleAction.Unload;
                };

                wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e)
                {
                    Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId);
                };

                wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
                {
                    // Display the unhandled exception.
                    Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
                        e.InstanceId, e.UnhandledException.Message);

                    Console.WriteLine("ExceptionSource: {0} - {1}",
                        e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);

                    // Instruct the runtime to terminate the workflow.
                    // Other choices are Abort and Cancel. Terminate
                    // is the default if no OnUnhandledException handler
                    // is present.
                    return UnhandledExceptionAction.Terminate;
                };
            }
        }
4

2 に答える 2

3

それは例外が別のスレッドでスローされる原因だと思います。これを確認してください:別のスレッドでスローされた例外をキャッチしてください

別のスレッドでスローされた例外は、デフォルトでは呼び出し元スレッドに渡されません。

編集: ワークフローの開始前にデリゲートを設定したいという印象がありますが、それでよろしいですか? その場合は、 を実行する前にへの割り当てwfApp.OnUnhandledExceptionなどを実行してください。wfApp.Run()

于 2012-08-17T16:58:39.550 に答える