WPF - Force ShowDialog will return

How can I get ShowDialog to return from a background thread?

I have a WPF application built on top of a C ++ / CLR framework that listens for messages sent by the framework. One particular message is so important that I need to close all current windows. However, if the modal Dialog (created by ShowDialog from my main window) is active and is waiting for user input, the window will not close because it is waiting for ShowDialog return. How to force modal Dialog to close and unlock code execution?

I tried installing DialogResult or calling Close , however it does not seem to work.

Edit: Dialog is created by my main window, which expects a return value, for example: (inside the click event handler in MainWindow ):

 Window modalDialog = new Window(); bool ret = (bool)modalDialog.ShowDialog(); if (ret == true) { // do stuff } else { // do some other stuff } 

When the structure sends a message (entering a different thread than the UI thread), I call MainWindow.Close() . The modal dialog box closes at this point, however, the verification code for the Dialog return value (after ShowDialog ) is still on the stack. Somehow this makes the main window not disappear.

+6
c # wpf showdialog modal-dialog
source share
3 answers

When a dialog receives a WM_CLOSE message, it sets a flag that causes ShowDialog to return after WM_CLOSE and all contained messages have been fully processed. Therefore, if you do Dispatcher.Invoke() in your thread to call MainWindow.Close() , ShowDialog() will not return until Dispatcher.Invoke() .

So, a simple solution is to return from Dispatcher.Invoke() to return ShowDialog() . If additional processing is required, immediately after that run another Dispatcher.Invoke() .

If you are performing a thread transition in any other way than Dispatcher.Invoke() , or if ShowDialog() does not return after calling Dispatcher.Invoke (), you may need to post some code surrounding your Window.Close() so that we could see what you are actually doing.

+3
source share

You cannot force ShowDialog to return, but you can force the window to listen to an event dispatched by your infrastructure, which may say that it is closing. In the example below, the Run Event has a Done flag, when it is completed, it closes.

 SomeGenerator IG = Manager.SomeGenerator(); SomeWindow PP = new SomeWindow(); IG.ProgressEvent += PP.ProgressEvent; IG.ExecuteAsync(); PP.ShowDialog(); 
+1
source share

Now I understand that I can not get ShowDialog to return and continue execution when I want. The event handler for the request that I receive from the framework should go out of context, so the call stack can be unwound, and the modal dialog can process the "Close" message.

As a result, I set a logical flag, asking to close the modaz, and then I will return from the framework event handler. Only at that moment ShowDialog will return, and I can check the set flag and continue it with the requested action.

0
source share

All Articles