1

下面是我的UI线程已经成功启动的新创建的线程代码(UI代码不在此处)。

当我单步调试时,我确实得到了这段代码。

当我正在执行的工作流没有任何问题时,代码会运行完成。但是,当工作流出现故障时(我正在使用有故障的工作流测试我的代码),此代码不会捕获在下面的语句中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 回答
0

我认为你只需要在 catch 之前处理 OnUnhandledException 事件(我看到你已经处理了这个事件,但是在你的 catch 块中)。

wfApp.OnUnhandledException 会让你捕捉到错误。

于 2014-06-12T18:29:39.293 回答