• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

CPP: non blocking message dialog?

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
 
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.
 
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.
 
never mind, hwnd of GetDesktopWindow() still blocks, although I'd imagine calling MessageBox with GetDesktopWindow() might help you be thread safe?
 
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.
 
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
 
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...
 
Back
Top