views:

316

answers:

6

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.

A: 

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

FlySwat
+1  A: 

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 Swangren
A: 

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
A: 

@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 Swangren
+1  A: 

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

Sijin
+1  A: 

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.

Tom A
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 Swangren