CPP: non blocking message dialog?

Sep 29, 2004
18,656
67
91
Right now we use:
MessageBox(messatgeText, "title", MB_ICONERROR | MB_OK);

This function blocks till OK is pressed however.

Is there a simple way to make this non-blocking? I know that wrapping the call to messageBox() in a thread would work, but I was wondering if C already has something built in to do this.

And since I almost made the mistake in writing this, I will remind all that I am talmking about non-blocking, not non-modal
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
You need to use a separate thread, subclass MessageBox and hook it's window proc so you can pump messages, or write your own message box.

The last one might actually be the simplest.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
So, in essence.. you DO want a modeless dialog, otherwise what would be the point? Or do you also want it to prevent input to the application, yet still want it to process something?

Honestly, the real way I see this happening is if you have something already being processed in a background thread, which a modal dialog isn't going to affect anyway.

My take, just do a modeless topmost dialog, have the parent give focus to the dialog, and until the dialog returns any time focus is restored to the app, have the app ignore input and give focus back to the dialog immediately. I'm sure it's not bulletproof, but probably simple enough, otherwise no matter what you're going to allow the app to have some sort of input.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
never mind, hwnd of GetDesktopWindow() still blocks, although I'd imagine calling MessageBox with GetDesktopWindow() might help you be thread safe?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
So, in essence.. you DO want a modeless dialog, otherwise what would be the point? Or do you also want it to prevent input to the application, yet still want it to process something?

Something like a cancelable operation is a potential application. An FTP transfer for example.

I really think writing a custom dialog is the only reasonable approach. If you're using Windows Forms then getting your thread back and calling yield every so often would work, since the event handler for the 'Ok' button will get a chance to fire and set a flag you can check. If it's a Win32 app then you need to run the message pump for the dialog using PeekMessage so that you can process whatever you need to process and watch for that ok button message.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Should the appropriate approach be something like this?


Create a thread
Give the thread the address of the function that's going to do the background work you want unblocked.
Start the thread.
Call the messagebox - it will be modal, and still block the function it's in, but not the work being done in the thread.
Call thread.join on the thread, thus making sure the work gets done.
Exit the sub
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: PhatoseAlpha
Should the appropriate approach be something like this?


Create a thread
Give the thread the address of the function that's going to do the background work you want unblocked.
Start the thread.
Call the messagebox - it will be modal, and still block the function it's in, but not the work being done in the thread.
Call thread.join on the thread, thus making sure the work gets done.
Exit the sub

Or create the thread and have IT block on your messageBox...