你可以告诉初学者为什么这个小型WPF应用程序在WorkflowTerminated事件触发后没有按预期关闭?使用的工作流程立即终止。 (使用WPF应用程序,.Net Framework 3.5)
public partial class MainWindow : Window
{
private WorkflowRuntime wfRuntime = new WorkflowRuntime();
public MainWindow()
{
InitializeComponent();
wfRuntime.WorkflowTerminated += (se, ev) => this.Close(); // this doesn't close the window
wfRuntime.WorkflowCompleted += (se, ev) => this.Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WorkflowInstance launcherWorkflow = wfRuntime.CreateWorkflow(typeof(InstallerWorkflow));
launcherWorkflow.Start();
}
}
答案 0 :(得分:6)
可能是因为回调是在另一个线程上。一个基本的解决方法是使用Environment.Exit(1);
要在UI线程上调用close函数,您应该使用:
wfRuntime.WorkflowTerminated += (se, ev) => {
// call back to the window to do the UI-manipulation
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
this.Close();
}));
};