MessageBox With Timeout


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

If a program or some part of a program can execute in the unattended mode, it may be useful to have a message box which would close on timeout, thus letting the program to continue. I would like to present here a function that does exactly this. It has the following declaration:

int MsgBoxWithTimeout(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, UINT uElapse, UINT uBlinkingTime);

The first 4 parameters are the same as in MessageBox. The message box will be closed in uElapse milliseconds unless there is any activity from the user. If the user does not close the message box, but either presses a key when it is in the foreground, or does some mouse action over its client or non-client area, the message box will wait for another uElapse milliseconds. uBlinkingTime milliseconds before closing, the message box caption will start blinking to attract user attention.

To be able to close the message box MsgBoxWithTimeout function spawns another thread. This new thread sleeps for a while to give the message box time to appear and then tries to find its window handle. To this end it uses EnumThreadWindows API function looking for the window with "#32770" class name. Then the function waits either for timeout, or the user to close the message box using WaitForSingleObject API. On timeout the function calls EndDialog API to close the message box.

Download demo project - 14 KB


  • Help me in Resolving this Error in WinXP Professional

    Posted by Legacy on 10/10/2003 07:00am

    Originally posted by: BKNAIDU

    Showing Error (Unhandled exception) Using this TOMessageBox code in Dialog based MFC application.. 

    Problem may be here with this lines of code , need for handling some kind of Unhandled exception while using DLG based GUI MFC application..

    unsigned int uThreadID;
    HANDLE hThreadHandle = (HANDLE)_beginthreadex(NULL, 0, TimeoutMsgBox,(void*)&rMyData, 0, &uThreadID);

  • Its been a while...

    Posted by Legacy on 07/08/2001 07:00am

    Originally posted by: Tom

    It has been a while since anyone said anything! Well, I donwloaded your program and found that if I clicked on the message box after it had loaded, the box did not go at all:

    i.) Was this meant to happen?
    ii.) Was the box supposed to close after 6 seconds of me clicking it?
    ii.)Is this your definition of 'user interaction?'

    Thanks it was good learning from it,


  • Modeless message box

    Posted by Legacy on 05/09/1999 07:00am

    Originally posted by: Pabitra Dash


    Can I create a modeless message box?
    Please help.


  • Great, but is it safe to use GWL_USERDATA?

    Posted by Legacy on 04/01/1999 08:00am

    Originally posted by: Mike Tsao

    I was trying to do the exact same thing a few nights ago but was dismayed to find with Spy that message boxes appear to use in the GWL_USERDATA field (that's where I, like you, was planning to store the old winproc to subclass the dialog proc). I assum that the MessageBox() function was using it. But your code appears to blow away the current value without any ill effects.

    I'm about to compile it and see if it works ok (which I'm sure it does), but have you figured out what's already in that field when it's passed to you?

  • What about logging?

    Posted by Legacy on 03/31/1999 08:00am

    Originally posted by: Brad Bruce

    It might be useful to log any messages that the user does not respond to (or even if they do<g>).  You
    could write the message to a log file.

    I have a dialog that acts similar to a message box but I put a read-only edit control on it for the message. That way the message can be copied from the dialog and sent in an e-mail etc.

  • Thank you. I got it.

    Posted by Legacy on 03/26/1999 08:00am

    Originally posted by: Masaaki Onishi


    Well, I appriciate for your help.
    This is a first time for me to migrate MFC with C code.

    Because I use application wizard, I got error message
    about precompiled file - stdafx.h. But I just added it.

    Now I am also working my individual interest -
    change button caption - font style -add check box on Messagebox.

    But I need to make some base class to make it general.

    If you are interested in this, please update your code.
    However, if you add more funtionality of your code,
    TOMsgBox parameter will expand more?

    -Masaaki Onishi-

  • Oops, C code!. How do we add your code to our MFC project?

    Posted by Legacy on 03/25/1999 08:00am

    Originally posted by: Masaaki Onishi


    Your code is very interesting even though I'm not familiar
    with C code.
    However, how do we add your code to our MFC project?
    I'v never made the combination of C code and MFC except
    I'm just lazy?

    Thank you for your post.
    -Masaaki Onishi-

  • Neat !

    Posted by Legacy on 03/24/1999 08:00am

    Originally posted by: Hans Wedemeyer

    Thanks, it filled a gap, MFC and the API needed this..

    Hans Wedemeyer

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date