4

So for the past day or so I have been fixing a bug that is caused by a modal dialog. I work on an application which communicates with the server through the Windows message pump. When I use ShowDialog() to show a modal form, the message pump is blocked and none of my messages are processed, yet they do build up in the queue (expected behavior).

However, I recently noticed that if a modal form is opened through a menu item's click event, the messages are pumped to the main form and processed. Does anyone know why these messages are not blocked when a modal form is shown through a menu item's click event?

EDIT: I should have noted that I am using C#. How about this; if no one can answer this question, can anyone tell me how to investigate this myself? The only thing that I can think of would be to look at the call stack. Unfortunately, this has not told me anything yet.

Ed S.
  • 122,712
  • 22
  • 185
  • 265

7 Answers7

1

Yes, I am calling ShowDialog() from the menu item's click event. In this case, the messages are pumped through the modal dialog to the main form.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

Try setting the same Owner/Parent for the dialog from the menu to the dialog that is showing expected message pumping behavior.

Sijin
  • 4,530
  • 21
  • 22
1

In general, your client UI should not block for long server operations. .Net makes it very easy to do server work using a BackgroundWorker thread. See this post for an example: Multi Threaded Import

The example is in VB but you can follow the links for a C# example.

Community
  • 1
  • 1
Tom A
  • 595
  • 7
  • 16
  • Thanks. The problem that I described was actually caused by me (of course) :). I had some other funny stuff going on in this project that I took over. – Ed S. Feb 04 '09 at 18:40
0

Are you calling ShowDialog() from the click event, or some other way?

FlySwat
  • 172,459
  • 74
  • 246
  • 311
0

What kind of menu control are you using? Could it be running on a separate thread from the one where the main form is running?

Chris Tybur
  • 1,612
  • 2
  • 23
  • 38
0

@Chris: I am just using the standard MenuStrip control. If it were running on a separate thread, I would then be interested in how it shows the form as modal. I experimented with showing the dialog from a separate thread as to not block the message queue, but I cannot specify the main form as a parent, so it is not really modal.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

I'm unclear at to what you mean by "the message pump is blocked".

What happens is that the ShowDialog does not return so the top-level message pump is waiting for your app to return from processing whatever event made it call ShowDialog; this is no different than if your handler for this even were grinding CPU. So, yes, in that sense, the message pump is blocked.

But the modal dialog itself runs its own message pump loop until the dialog is closed, which should process messages just as the main loop does, so I don't understand why any messages should be building up in the queue.

This message loop processes messages for all windows because it must e.g. allow other windows of the app to paint themselves properly.

You might try looking at the callback stack (from the ShowDialog call down to the root of the stack) and compare how it looks when "things are working as they should" and when "things aren't working". It may be something subtle like whether you got to the ShowDialog call through message dispatching or message preprocessing (which I have just found makes a difference when you call ContextMenuStrip.Show)

Kevin Martin
  • 116
  • 8