WPF window does not close

Could you tell beginners why this small WPF application does not close as intended after WorkflowTerminated is triggered? The workflow used immediately ends. (using WPF application, .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(); } } 
+6
source share
1 answer

Probably because the callback is in a different thread. The main workaround is to completely terminate the application using Environment.Exit(1);

To call the close function in the user interface thread, you should use:

 wfRuntime.WorkflowTerminated += (se, ev) => { // call back to the window to do the UI-manipulation this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { this.Close(); })); }; 
+6
source

Source: https://habr.com/ru/post/885889/


All Articles