MessageBox With Timeout

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



Comments

  • Help me in Resolving this Error in WinXP Professional

    Posted by Legacy on 10/10/2003 12: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);
    if(hThreadHandle)
    CloseHandle(hThreadHandle);

    Reply
  • Its been a while...

    Posted by Legacy on 07/08/2001 12: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,

    Tom

    Reply
  • Modeless message box

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

    Originally posted by: Pabitra Dash

    Hi,

    Can I create a modeless message box?
    Please help.

    Regards,
    Pabitra

    Reply
  • Great, but is it safe to use GWL_USERDATA?

    Posted by Legacy on 04/01/1999 12: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?

    Reply
  • What about logging?

    Posted by Legacy on 03/31/1999 12: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.

    Reply
  • Thank you. I got it.

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

    Originally posted by: Masaaki Onishi

    Hi.

    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?

    Regards.
    -Masaaki Onishi-

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

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

    Originally posted by: Masaaki Onishi

    Hi.

    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
    DLL.
    I'm just lazy?

    Thank you for your post.
    -Masaaki Onishi-

    Reply
  • Neat !

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

    Originally posted by: Hans Wedemeyer

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

    Hans Wedemeyer

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • On-demand Event Event Date: July 22, 2014 In this WhatWorks analysis, John Pescatore examines a use case where end users had local administrative rights on their PCs and it had gotten out of hand for this Fortune 500 Energy and Utilities company. The compelling event that prompted the company to reexamine this situation was the migration to Windows 7. In Windows XP, a custom tool that allowed users one of three levels of administrative rights to their workstations would need to be replaced during the Windows …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds