Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to pop up a message box without pausing the program?

257 views
Skip to first unread message

xuanp...@gmail.com

unread,
Sep 26, 2012, 8:42:47 AM9/26/12
to
The program would pause if I use messagebox. Is there any as convenient way to show some message to the user without blocking the program? Thanks.

Christian ASTOR

unread,
Sep 26, 2012, 8:54:20 AM9/26/12
to
xuanp...@gmail.com a écrit :
> The program would pause if I use messagebox. Is there any as convenient way to show some message to the user without blocking the program? Thanks.

There are several ways, like IUserNotification for example.

Deanna Earley

unread,
Sep 26, 2012, 9:01:14 AM9/26/12
to
On 26/09/2012 13:42, xuanp...@gmail.com wrote:
> The program would pause if I use messagebox. Is there any as convenient way to show some message to the user without blocking the program? Thanks.

Putting anything that is "sensitive" on another thread.

A message box blocks the thread of execution but it still runs a message
pump so messages will be received.

Putting any work on another thread means it's unaffected by any UI issues.

--
Deanna Earley (dee.e...@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored. Please reply to the
group.)

ScottMcP [MVP]

unread,
Sep 26, 2012, 9:53:17 AM9/26/12
to
On Wednesday, September 26, 2012 8:42:47 AM UTC-4, (unknown) wrote:
> The program would pause if I use messagebox. Is there any as convenient way to show some message to the user without blocking the program? Thanks.

The built in message box is modal. So it blocks the program "by design." Assuming that your program is a GUI it should be rather simple to add a custom pop up dialog that is nonmodal.

xuanp...@gmail.com

unread,
Sep 26, 2012, 10:46:46 AM9/26/12
to
OK. Thanks. Below is my code and it works.
===========================================

DWORD WINAPI thread(LPVOID lpParam)
{
char *message;
char *title;

title = ((two_strings *) lpParam)->s1;
message = ((two_strings *) lpParam)->s2;

MessageBox(NULL, TEXT(message), TEXT(title), MB_OK | MB_ICONWARNING);

return 0;
}

void show_message(char *title, char *msg)
{
two_strings message;
HANDLE hthread = 0;

strcpy(message.s1, title);
strcpy(message.s2, msg);

hthread = CreateThread(NULL, 0, thread, &message, 0, NULL);
if (hthread == NULL)
ExitProcess(1);
CloseHandle(hthread);
}




On Wednesday, 26 September 2012 09:01:17 UTC-4, Deanna Earley wrote:
> On 26/09/2012 13:42,
>
> > The program would pause if I use messagebox. Is there any as convenient way to show some message to the user without blocking the program? Thanks.
>
>
>
> Putting anything that is "sensitive" on another thread.
>
>
>
> A message box blocks the thread of execution but it still runs a message
>
> pump so messages will be received.
>
>
>
> Putting any work on another thread means it's unaffected by any UI issues.
>
>
>
> --
>
>

ScottMcP [MVP]

unread,
Sep 26, 2012, 12:40:28 PM9/26/12
to
There is a fatal flaw in your thread creation code. The 'message' variable is created on the stack, and then you pass its address to the thread. But that variable is destroyed when the function returns, so the thread may receive a pointer that has become invalid. (The thread is not guaranteed to start immediately after you call CreateThread. It can start some time later.)

The fix is to allocate 'message' with new or malloc. Then free it in the thread function.


Charlie Gibbs

unread,
Sep 27, 2012, 1:39:29 AM9/27/12
to
In article <5c08fb50-bcce-4ebb...@googlegroups.com>,
If you just want to display a message without needing an OK/cancel
response, you could create a small window with your message in it,
and just leave it there until the user closes it, or perhaps set
a timer to close it after a few seconds.

--
/~\ cgi...@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!

Deanna Earley

unread,
Sep 27, 2012, 3:47:09 AM9/27/12
to
On 26/09/2012 15:46, xuanp...@gmail.com wrote:
> OK. Thanks. Below is my code and it works.
> ===========================================
>
> DWORD WINAPI thread(LPVOID lpParam)
> {
> char *message;
> char *title;
>
> title = ((two_strings *) lpParam)->s1;
> message = ((two_strings *) lpParam)->s2;
>
> MessageBox(NULL, TEXT(message), TEXT(title), MB_OK | MB_ICONWARNING);
>
> return 0;
> }
>
> void show_message(char *title, char *msg)
> {
> two_strings message;
> HANDLE hthread = 0;
>
> strcpy(message.s1, title);
> strcpy(message.s2, msg);
>
> hthread = CreateThread(NULL, 0, thread, &message, 0, NULL);
> if (hthread == NULL)
> ExitProcess(1);
> CloseHandle(hthread);
> }

That's not exactly what I meant.
You should keep all UI on the main UI thread.
Any time sensitive or long running code should be on it's own thread.

--
Deanna Earley (dee.e...@icode.co.uk)

xuanp...@gmail.com

unread,
Sep 27, 2012, 10:42:11 AM9/27/12
to
That's what I need: pop up a window with message (date) on it. But don't know how to do it with only several lines of code.

On Thursday, 27 September 2012 00:40:03 UTC-4, Charlie Gibbs wrote:


>
>
>
> > The program would pause if I use messagebox. Is there any as
>
> > convenient way to show some message to the user without blocking
>
> > the program? Thanks.
>
>
>
> If you just want to display a message without needing an OK/cancel
>
> response, you could create a small window with your message in it,
>
> and just leave it there until the user closes it, or perhaps set
>
> a timer to close it after a few seconds.
>
>
>
> --
>
>

Charlie Gibbs

unread,
Sep 27, 2012, 12:34:00 PM9/27/12
to
In article <39d1d46b-1dd8-410e...@googlegroups.com>,
xuanp...@gmail.com (xuanpingren) writes:

> That's what I need: pop up a window with message (date) on it.
> But don't know how to do it with only several lines of code.

It's just like creating any other window, i.e. one of those
several lines of code is CreateWindow().

--
/~\ cgi...@kltpzyxm.invalid (Charlie Gibbs)
0 new messages