Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不捕获工作流异常?_C#_Multithreading_Exception Handling_Workflow Foundation - Fatal编程技术网

C# 为什么不捕获工作流异常?

C# 为什么不捕获工作流异常?,c#,multithreading,exception-handling,workflow-foundation,C#,Multithreading,Exception Handling,Workflow Foundation,下面是我的UI线程成功启动的新创建的线程代码(UI代码不在这里) 当我在调试过程中只进行了一步,我就得到了这段代码 当我正在执行的工作流没有任何问题时,代码将运行到完成。但是,当工作流出错时(我正在使用错误的工作流测试代码),此代码不会捕获在下面的wf.Run()语句中发生的WorkflowException 我想我有下面的工作流执行异常处理代码??你能告诉我我做错了什么吗?谢谢 public void ThreadRun () { AutoResetEven

下面是我的UI线程成功启动的新创建的线程代码(UI代码不在这里)

当我在调试过程中只进行了一步,我就得到了这段代码

当我正在执行的工作流没有任何问题时,代码将运行到完成。但是,当工作流出错时(我正在使用错误的工作流测试代码),此代码不会捕获在下面的
wf.Run()
语句中发生的
WorkflowException

我想我有下面的工作流执行异常处理代码??你能告诉我我做错了什么吗?谢谢

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;
                };
            }
        }

我认为,这是因为异常被抛出到另一个线程中。选中此项:

默认情况下,在不同线程中引发的异常不会传递给调用线程


编辑:我的印象是,您希望在工作流开始之前设置代理,对吗?如果是这样,在执行
wfApp.Run()

之前,先执行对
wfApp.onunandledexception
等的赋值,我认为您只需在捕获之前处理OnUnhandledException事件(我看到您已处理此事件,但在您的捕获块中)


wfApp.OnUnhandledException将允许您捕获错误。

顺便问一下,为什么在捕获异常后设置委托?…我还尝试在try{wfApp.Run()之前设置委托)还有。我想我的问题是关于以下情况下的代码结构。线程A启动线程B。线程B执行工作流>在哪里以及如何捕获工作流问题?在线程A或线程B中。还有,我该如何做。总之,我的问题是关于结构,以及我应该将委托代码放在哪里。我是一名经验丰富的CED程序员在C++中,但对所有这些都是新的。谢谢。在运行WFApp之前,设置OnUnWord命令的回调就足够了。每当有一个未处理的异常时,调用这个回调。检查委托就像一个C++中的方法指针,如果这使事情变得清晰。谢谢。下面的代码是Belo吗?到UI线程的NG?:wfApp.Completed=delegate(WorkflowApplicationCompletedEventArgs e)等。是的。它正在设置回调,因此委托中的代码不会在UI中执行。这基本上只是告诉wfApp“如果存在
未处理的异常
,请执行以下操作,然后执行以下操作”1)我必须在wfApp.Run()之前设置委托2) 委托的设置必须在第二个线程中完成,我认为这不是UI线程,因此它是另一个线程(无论术语如何称呼它?)。3如果出现异常,我只能在“try”之后的catch语句中捕获它我在其中放了一个MessageBox.Show.SUMMARY:我仍然不知道为什么意外异常的代码从未被命中,尽管我执行了一个错误的工作流。catch语句被执行,但未处理异常的委托代码没有被执行。当我执行一个无错误的工作流时,已完成异常的委托代码被执行。@user1298925工作流中的异常与InvalidWorkflowException之间存在差异,InvalidWorkflowException在某种程度上是外部的,因为它不是由工作流中的任何内容引发的。请尝试向工作流中添加一个引发活动,如本例所示,您将调用OnUnhandledException。