How is it possible to show two modal message boxes in the same thread?

  • Thread starter Thread starter Andrew Baker
  • Start date Start date
A

Andrew Baker

OK this has me perplexed, puzzled and bamboozled!

I have a remoting service which I displayed a message box in. I then
wondered what would happen if a client made a call to the service
while the message box was being displayed. Since the call comes in on
a different thread the client call was not blocked. This seemed to
make sense to me.

Next I thought, what will happen when the client calls into the server
if I try to use a delegate to call back onto the UI thread? To my mind
I thought I should be blocked, but I was able to perform work on the
UI thread while the message box was displayed. So finally, I thought I
wonder what happens if I then also try an display another message box
on the UI thread and sure enough I was able to display two modal
message boxs on the UI thread (I put the current thread ID in the
message boxes just to make sure it was on the same thread).

I don't understand this behaviour as I think a modal dialog should
block a thread... But I am clearly wrong, I just don't know why?!

andrew
www.vbusers.com
 
A MessageBox isn't really the same thing as a modal dialog. You can keep
poping up MessageBoxes until theres loads stacked up on the screen ;)

to show a modal dialog look at Form.ShowDialog and show a form.

(e-mail address removed) (Andrew Baker) wrote in
 
Keep in mind that neither modal dialogs or message boxes (which are
essentially a type of modal dialog) really block the current thread. The
thread is processing a message loop the whole time -- if it were blocked
with respect to the parent window, the window wouldn't be able to redraw
itself as you move the modal dialog over top of it.

The way I think of it is that each thread running a message loop has a
stack of modal dialogs. Any time a modal dialog is opened (including message
boxes), it's pushed onto the stack. All of these dialogs share the same
message pump, but that pump only allows certain messages (i.e. non-input
related) to reach windows which are not the top-most in the stack (e.g.
paint messages can get through but click events and such don't). When the
moda dialog is closed, it's popped off the stack and the next one down
becomes the "active" dialog.

So, in your experiment, you should have noticed that even though you had two
message boxes up on the same UI thread, one was "active" and the other
couldn't be closed until the active one was closed.

Ken
 
Back
Top