以下是我的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;
};
}
}
答案 0 :(得分:3)
我认为这导致异常被抛出在另一个线程中。检查一下:catch exception that is thrown in different thread
默认情况下,不会将另一个线程中抛出的异常传递给调用者线程。
编辑:我觉得您希望在工作流程开始之前设置代理,这是正确的吗?如果是,请执行wfApp.OnUnhandledException
之前的<{1}}和之前的分配。{/ 1>}。
答案 1 :(得分:0)
我认为你必须在catch之前处理OnUnhandledException事件(我看到你在catch块中处理了这个事件)。
wfApp.OnUnhandledException可以捕获错误。