tags:

views:

268

answers:

3

In my application, I have a task which is running on a background thread. I need a notification in the background thread when ever a MessageBox or any modal dialog is displayed in the UI thread.

Though I can do it manually by calling some function before displaying the MessageBox, but it will be great if I dont have to.

For e.g.:

backgroundThread.MessageShown(); // I do not want to call this explicitly every time!
MessageBox.Show("Task halted!");

I am guessing there might me some message which can be hooked on to. Even in the main GUI thread, is there any message/event that get fired just before a modal dialog is shown?

Okay, here is the requirement. I have some tasks which are done on the UI thread, and I have to show progress on a separate dialog, which is been shown on a worker thread. I understand that it should be the tasks which must be done on worker thread, but the current scenario cannot be changed for the time being.

Every thing is working fine, except for one glitch - if a message box is shown in the UI thread, it gets hidden below the progress dialog. So the user never gets to know that UI is waiting for an input. I need a way to get notified that a modal dialog box has been shown and I should hide the progress dialog.

Right now, I have to hide it explicitly just before every call to MessageBox.

I hope that explains.

+2  A: 

create your own messagebox that fires an event when calling Show?

PoweRoy
Okay, a little more information: I will be using this in an existing application, so it will be terrible to go around and change every call!And further, as I said, I want the notification for _any_ modal dialog.
nullDev
Hmm somekind of wrapper that you call:MBWrapper(MB_OK, "text"); MBWrapper will fire the event and call MessageBox.Show ?
PoweRoy
+3  A: 

Set up a CBT Hook. Then you'll get notification of all created, activated, deactivated and destroyed windows. Then use GetWindowClass to check if the hWnd created/activated is in fact a MessageBox.

danbystrom
Thanks danbystrom, this is exactly what I was looking for!
nullDev
Glad I could help! Thanks for the +100. :-)
danbystrom
A: 

If the parent of your MessageBox is TopMost, then your MessageBox will be TopMost. So, something like ...

MessageBox.Show( new Form() { TopMost = true }, "Message and so forth ..." );

This might also work for you ...

DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, String text, String caption, uint type);

And pass MB_TOPMOST as the type.

JP Alioto