5

could you tell a beginner why this small WPF-application is not closing as intended after the WorkflowTerminated event fires? The used workflow just terminates immediately. (using a 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(); } } 
2
  • When setting a breakpoint in the terminated-eventhandler, is it even executed? Commented Apr 14, 2011 at 7:28
  • 1
    Good question by the way, this haunted me before and was very hard to figure, +1 Commented Apr 14, 2011 at 7:49

1 Answer 1

6

Probably because the callback is on another thread. A basic workaround is to terminate the application altogether using Environment.Exit(1);

To call the close function on the UI 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(); })); }; 
Sign up to request clarification or add additional context in comments.

7 Comments

My Application object doesn't have an exit() method. Propably because it is an WPF application? I tried Application.Current.Shutdown() which doesn't help. Still doesn't close...
How could I post the terminate event to the right thread (GUI thread)?
Environment.Exit() works, the application is closing. But it feels kind of dirty ;-) Is there a cleaner way, to reach my goal (i.e. handling the event in the right thread, so Close() works as intended?). Nevertheless thanks for your help so far!
Yep I updated the answer to callback the Close() method on the UI thread so UI windows closes right.
Just fixed a typo by the way (forgot the second closing parenthesis before semicolon)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.