1

When you show a MessageBox or another type of Dialog like a OpenFileDialog or even a window you made and called with ShowDialog() what is happening inside that method to keep the app from continuing while it waits for DialogResult to be set?

public static bool MyDialog.Show(string message) {
   //what happens here to keep the program from continuing?

   //Oh! DialogResult was finally set...
   return DialogResult;
}


private void OkButton_Click(object sender, RoutedEventArgs e) {
    DialogResult = true
}

Its not like you can just put a while(true) loop and wait for the user to hit ok, so whats actually happening in the background?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
bwoogie
  • 4,339
  • 12
  • 39
  • 72
  • See https://stackoverflow.com/a/41769483/1086121 (and other documentation on `DispatcherFrame`) – canton7 Feb 14 '19 at 17:17
  • DispatcherFrame is a c# thing and doesn't really answer the question. Modal windows are controlled through the underlying Win32 API that c# is calling.. In the case of DispatcherFrame.. that is what IT is calling. Windows does this through a mechanism called a "Modal Window". https://en.wikipedia.org/wiki/Modal_window which gets invoked as a flag for the the underlying "CreateWindow" call. Windows does the work.. – Señor CMasMas Feb 14 '19 at 17:19
  • @SeñorCMasMas I think there are two interpretations of the phrase "to keep the app from continuing while it waits for DialogResult to be set?". I assumed he was asking how the call to `Window.ShowDialog` didn't return until the dialog had been closed, yet dispatcher messages were still processed, the answer to which is absolutely "Dispatcher frames". I think you're answering the question "How come the user can only interact with the modal window, and not its parent", which is absolutely further down in the winapi – canton7 Feb 14 '19 at 17:31
  • It is a `while` loop, which I call [message pump](https://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows). – Dialecticus Feb 14 '19 at 17:42
  • The while loop IS a message pump! :^) I didn't mean to diss anybody.. The message pump happens on non-modal windows too.. ALL windows have a pump. – Señor CMasMas Feb 14 '19 at 17:47
  • Yep, there is literally a while loop for each window that is called a message pump. Which is a bit of an odd name and particularly so if you don't know what a windows message is. When you interact with windows - click or whatever - that generates a windows message. If you click a button then it's that message some bit of code listens for and drives what you ghink of as the click event. Maybe this explanation is clearer httpss://social.msdn.microsoft.com/Forums/vstudio/en-US/3f37a652-b558-4342-ade9-ad5ce68c9d01/why-does-showdialog-not-block-the-ui-thread-completely?forum=netfxbcl – Andy Feb 14 '19 at 20:29

0 Answers0