WPF window not closing

Total Post:108

Points:756
WPF 
WPF
 1465  View(s)
Ratings:
Rate this:

I want to close a window in WPF but it is not close below I am providing you the code sample which I use for closing the window.

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();
    }
}

  1. Post:100

    Points:700
    Re: WPF window not closing

    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();
        }));
    };

      Modified On Mar-30-2018 05:32:34 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!