• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1355
  • Last Modified:

Nonblocking MessageBox

I would like to use a MessageBox in C++ Builder, that would not block the execution of the program even if the user does not respont to the MessageBox window.
Should be something like the Outlook "Incoming mail" notification MessageBox.
  • 5
  • 4
1 Solution
The messagebox in Builder is an interface to the windows API MessageBox() function.  That function will block the current thread.  Its design requires it to block the thread because the function needs to return information to the program  (i.e. which button was pressed.) and because the idea is that it presents information to the user and gets a response from the user that determines what the program should do ("okay to delete file?"  the program needs an answer to this before it can go on.)

If you don't want to block the thread then most likely you really shouldn't be using MessageBox.   You can simply use a window of another type--probably a type you create.

However, if you really do want to use it, you can, just start a new thread and call message box from that thread.

Are you familar with threads?

The problem with this approach would be getting infromation back from that thread that indicates what button the user pressed.  However, most likely you don't need this--otherwise you woudl want it to block the thread..  However, if you do need to get information back, let me know, it can be done.

Let me know if you have any questions.
I should point out something.   You mention an "incoming mail" message window.   There is an important difference between that type of window and the MessageBox type of window.   the Messagebox window is closed by the user.  The incoming mail one is closed when the mail arrives.      That is a very important difference in how the windows function.  

If you need the window to close from sources other than the user clicking on a close button, then that is even further evidence that you really don't want to use MessageBox() for this.

What is this window for?
octiAuthor Commented:

You are wrong about "Incoming mail" notification window. Actually the user closes it (only). If not, it remains on the desktop.

So there is no difference between API's MessageBox and Microsoft Outlook's notification message windows - from this point of view.

In the mean while you can access your calendar, private folders, can receive other emails, without responding to the message window.

I need this particular MessageBox to show a message that comes from the socket (asynchronously). After the message is shown, my program should work as before, even if the user does not respond to the message. (Like in Microsoft Outlook)

The message text length could vary, so I don't want to deal with sizing the Message Window that I would create my self-etc. since the MessageBox API already solves this.

I did not necessarily refer to Builders wrapped MessageBox function, (Application->MessageBox) but all kind of message dialogs, that could be called from C++ Builder.

If there is really no other method then separated thread, I will accept your answer, but this method may not worth using it.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

>> You are wrong about "Incoming mail" notification window.
I don't use outlook.   so I had no way of knowing.    However most other mail systems--and inmost other similar situations the program closes the window.  For good reason too!

>> In the mean while you can access your calendar, private folders, can
>> receive other emails, without responding to the message window.
That is a huge difference though.  

There are two possible solutions.  

You can start a seperate thread and use Messagebox or you can create a regular (non-modal) window for doing this.

>>  this method may not worth using it.
why not?
MessageBox is handy because it sizes itself dynamically based on the text and it displays an icon and it provides a way to use different sets of buttons.

Alas, the standard MessageBox is always Modal.  But it is not particularly hard to make a regular dialog look a lot like a MessageBox and then use Create() rather than DoModal (or whatever Borland uses).  That way, you can put up as many of these 'simulated MessageBox' dialog boxes as you want -- without the headache of making a new thread for each one.

-- Dan
octiAuthor Commented:
I promised to accept your answer if there is no other solution then thread.
Still the method is not worth using it becase it's wrong thinking. (Use thread to show a modal dialog as non-modal - rather show a non-modal one)

About "incoming mail"...if you don't use outlook don't make "smart" comments.
>> About "incoming mail"...if you don't use outlook don't make "smart" comments.

Look most operations like that have a limited lifetime.  When the operation is done, the window closes.   Like when you copy files in explorer it shows you a dialog during the copy process.  You don't have to close this dialog--thank god--it closes automatically when the operation is done.  Most mail programs do this as well.   Lots of programs do this.   I can't imagine why outlook doesn't.    but you may want to consider it for your program.   Is there a good reason wjy your window should stay open after the operation completes?   If not, why make the user go to the effort of closing it?

>> Still the method is not worth using it becase it's wrong thinking.

It provides the right behavior and has no real drawbacks--unless you plan on opening up many of these windows all at the same time.  its easy to implement and meets all of your needs.    I don't see the problem.

What are your reservatiosn agbout this?
octiAuthor Commented:
I already mentioned, that I will use the messagebox to display incoming messages from a communication endpoint (a socket).
It needs the user interaction, because the user should read the message, before the MessageBox closes.

In the mean while, the program should respond to other network or user events.

And yes, I plan to open more this kind of windows at the same time, because it's up to the communication endpoint how many messages will arrive.
It doesn't sound like a :"notification" window to me, but more like the actual mail window.

How many of these windows do you think would be open at a time?   And what OS (OSs) is this for?

If it is for 95/98, its probably not a good to try if you expect to have more than 6 or 7 open at a time.   Under NT you aren't going to have much problem unless a lot of them are open.
octiAuthor Commented:
Actually this window contains both in the same time. It's a notification, but also contains the short text message coming from a user.
After the text was read the window can be closed by the user.
(Like net send ...service under NT)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now