title: WPF - Force ShowDialog to return
body: <p>How can I force <code>ShowDialog</code> to return from a background thread? </p>
  
  <p>I have a WPF application built on top of a C++/CLR framework which is listening to messages sent by the framework. One particular message is so important that I need to close all the current windows. However, if a modal <code>Dialog</code> (created by <code>ShowDialog</code> from my main window) is active and awaiting user input, the window will not close because it is waiting for <code>ShowDialog</code> to return. How can I force the modal <code>Dialog</code> to close and unwind code execution?</p>
  
  <p>I tried setting the <code>DialogResult</code> or calling <code>Close</code> however that doesn't seem to work.</p>
  
  <p>Edit: The <code>Dialog</code> is created by my main window which expects a return value, like so:
  (inside a click event handler in <code>MainWindow</code>):</p>
  
  <pre><code>Window modalDialog = new Window();
  bool ret = (bool)modalDialog.ShowDialog();
  if (ret == true)
  {
   // do stuff
  }
  else
  {
   // do some other stuff
  }
  </code></pre>
  
  <p>When the framework sends a message (coming in on a different thread than the UI thread), I call <code>MainWindow.Close()</code>. The modal dialog gets closed at this point, however the code checking the return value of the <code>Dialog</code> (after <code>ShowDialog</code>) is still on the stack.  Somehow this forces the main window to not disappear.</p>