Nonblocking MessageBox

Posted on 2002-06-05
Medium Priority
Last Modified: 2008-02-01
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.
Question by:octi
  • 5
  • 4
LVL 22

Expert Comment

ID: 7056330
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.
LVL 22

Expert Comment

ID: 7056339
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?

Author Comment

ID: 7056476

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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

LVL 22

Accepted Solution

nietod earned 150 total points
ID: 7056802
>> 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?
LVL 49

Expert Comment

ID: 7057105
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

Author Comment

ID: 7058613
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.
LVL 22

Expert Comment

ID: 7058858
>> 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?

Author Comment

ID: 7058867
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.
LVL 22

Expert Comment

ID: 7058965
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.

Author Comment

ID: 7058976
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

568 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question