---
title: "WPF window not closing"  
description: "WPF window not closing"  
author: "Anonymous User"  
published: 2013-07-19  
updated: 2013-07-19  
canonical: https://www.mindstick.com/forum/1333/wpf-window-not-closing  
category: "wpf"  
tags: ["wpf"]  
reading_time: 1 minute  

---

# WPF window not closing

I want to [close](https://yourviews.mindstick.com/story/1687/rubbing-hands-close-up-benefits) a [window](https://www.mindstick.com/forum/161285/how-does-the-window-console-object-work-in-javascript) in [WPF](https://www.mindstick.com/forum/304/wpf) but it is not close below I am providing you the [code sample](https://www.mindstick.com/forum/33394/under-what-condition-could-the-code-sample-below-crash-your-application-how-would-you-modify-the-code-to-avoid-this-potential-problem) 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();    }}
```

## Replies

### Reply by Vijay Shukla

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();    }));};
```


---

Original Source: https://www.mindstick.com/forum/1333/wpf-window-not-closing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
