【发布时间】:2012-08-17 16:36:09
【问题描述】:
以下是我的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()) 之前设置代表
-
也是如此。我想我的问题是关于以下情况下的代码结构。线程 A 启动线程 B。线程 B 执行工作流>我在哪里以及如何捕获工作流问题?在线程 A 或线程 B 中。另外,我该怎么做。总之,我的问题是关于结构,以及我应该将委托代码放在哪里。我是一位经验丰富的 C++ 程序员,但对所有这些都是新手。谢谢。
-
在运行 wfApp 之前为 OnUnhandledException 设置一个回调就足够了。只要有未处理的异常,就会调用此回调。检查msdn.microsoft.com/en-us/library/…
-
delegate 很像 C++ 中指向方法的指针,如果这样可以更清楚的话。
标签: c# multithreading exception-handling workflow-foundation