Click here to Skip to main content
15,861,125 members
Articles / Desktop Programming / MFC
Article

A thread-safe timed message box

Rate me:
Please Sign up or sign in to vote.
3.43/5 (4 votes)
12 Feb 2001 217.7K   3.4K   50   28
The system Message Box that is closed atuomatically after some time

What is the code for

This is for using the systems MessageBox (no extra dialog creation needed). After a time with no user-response the dialog is automatically closed. An optional text showing the time left to interact is provided. It's also a replacement for MSDN Q181934 (doesn't work)

Basics

Before calling MessageBox, a timer is installed. In the timer's callback-procedure the message-text from the box is replaced (optional).

When the box should be closed, a WM_COMMAND is posted to the MessageBox Window.

Timer-Callback

I decided to collect all the information corresponding to the MessageBox (title, Message, Flags ...) in a class. Then there is the problem that only a static timer-callback-procedure can be installed with the SetTimer()-function: one can not access the class-members (as it is static).

To solve this problem, I inserted a Map to store the classes corresponding to a timer-id:

Header:

class CDlgTimedMessageBox
{
public:
    // ...
    UINT         ShowMessageBox(BOOL *pbStoppedByUser=NULL);
    // ...
    static void  CALLBACK GlobalTimerProc(HWND hwnd, UINT uiMsg, 
                                          UINT idEvent, DWORD dwTime);
    void         LocalTimerProc(void);
    // ...
    static       CMapPtrToPtr        m_mapTimerIdToClassMe;

    // to call the messagebox within one line !
    static UINT  TimedMessageBox(...);

protected:
    UINT         m_idTimer;
};

Source:

UINT CDlgTimedMessageBox::ShowMessageBox(BOOL *pbStoppedByUser)
{
    // ...
    m_idTimer = ::SetTimer(NULL, 0, 1000, 
                    (TIMERPROC) CDlgTimedMessageBox::GlobalTimerProc);
    CDlgTimedMessageBox::m_mapTimerIdToClassMe.SetAt((void*)m_idTimer, 
                                                     this);
    // ...
    ::MessageBox(...)
    // ...
}

void CALLBACK CDlgTimedMessageBox::GlobalTimerProc(HWND hwnd, 
                               UINT uiMsg, UINT idEvent, DWORD dwTime)
{
    CDlgTimedMessageBox    *pMe = NULL;
    
    // Find the corresponding class by the timer-id
    CDlgTimedMessageBox::m_mapTimerIdToClassMe.Lookup((void*)idEvent, 
                                                       (void *&) pMe);
    
    if( pMe!=NULL )
        pMe->LocalTimerProc();
}

void CDlgTimedMessageBox::LocalTimerProc(void)
{
    // find the Message-Box-window

    // Calculate time since start
    
    if( too long running )
    {
        // Stop MessageBox
    }
    else
    {
        // replace text of MessageBox    
    }    
}

Finding the MessageBox

hWnd = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
while( (hWnd!=NULL) && (m_hMsgBox==NULL) )
{
    pWnd = CWnd::FromHandle(hWnd);
    pWnd->GetWindowText(title);

    if( AfxIsDescendant(m_hParent, hWnd) && ::IsWindowVisible(hWnd) && 
        (m_Title.CompareNoCase(title)==0) )
    {
        m_hMsgBox = hWnd;
        break;
    }
    
    hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);
}

 

Put it in one function

There is one function you can use to call the messagebox without creating a class-instance: CDlgTimedMessageBox::TimedMessageBox() does the rest for you!

UINT CDlgTimedMessageBox::TimedMessageBox(UINT flags, LPCTSTR ptszMessage, 
                          LPCTSTR ptszTitle, 
                          DWORD dwTimeout, UINT dDefaultReturn,
                          LPCTSTR ptszMessageTimer, HWND hwndParent, 
                          BOOL *pbStoppedByUser)
{
    CDlgTimedMessageBox    msgBox(flags, ptszMessage, ptszTitle, 
                                  dwTimeout, dDefaultReturn, 
                                  ptszMessageTimer, hwndParent);

    return msgBox.ShowMessageBox(pbStoppedByUser);
}

You can pass a BOOL * to get the info, if the user has pressed a button.

 

Thread-Safe

The map-access is enclosed by a CCriticalSection.

Sample

BOOL	stoppedByUser;
UINT	erg;

erg  = CDlgTimedMessageBox::TimedMessageBox(MB_YESNO|MB_ICONHAND, 
		"Please press a button", 
		"box-title", 
		5000, IDYES, 
		"\nin the next %lu sec !", 
		NULL, &stoppedByUser);

History

5.11.2000     posted to Code Project
14.11.2000   bugfixes for NT4
05.02.2001   bug reported when created with MBYES: defbutton then is IDCANCEL
05.02.2001   global function went to class-scope

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Sudhindra Sathya19-Nov-10 1:26
Sudhindra Sathya19-Nov-10 1:26 
QuestionHow to show other icons in the messagebox.. Pin
Jigar Mehta23-Nov-04 20:48
Jigar Mehta23-Nov-04 20:48 
GeneralCompiling errors Pin
FlyingDancer7-Oct-03 21:11
FlyingDancer7-Oct-03 21:11 
GeneralRe: Compiling errors Pin
cristitomi21-Mar-07 6:35
cristitomi21-Mar-07 6:35 
Generaltimed out MessageBox Pin
Xeena2-Oct-02 6:53
Xeena2-Oct-02 6:53 
Generalbottons Pin
17-Jun-02 5:29
suss17-Jun-02 5:29 
GeneralIf you like this then.... Pin
12-Mar-02 7:23
suss12-Mar-02 7:23 
General"Fire and Forget" addition Pin
M.Lansdaal12-Feb-02 14:53
M.Lansdaal12-Feb-02 14:53 
GeneralTimer Pin
12-Nov-01 2:59
suss12-Nov-01 2:59 
GeneralRe: Timer Pin
mloibl12-Nov-01 21:10
mloibl12-Nov-01 21:10 
GeneralRe: Timer Pin
22-Nov-01 5:04
suss22-Nov-01 5:04 
GeneralMemory Leaks Pin
eljkmw22-Aug-01 17:53
eljkmw22-Aug-01 17:53 
GeneralRe: Memory Leaks Pin
Alan Adams28-Oct-04 11:04
Alan Adams28-Oct-04 11:04 
QuestionHOW? Pin
6-Jul-01 7:00
suss6-Jul-01 7:00 
AnswerRe: HOW? Pin
mloibl17-Jul-01 20:50
mloibl17-Jul-01 20:50 
GeneralMissing include Pin
6-Jul-01 4:52
suss6-Jul-01 4:52 
GeneralAlways on top Pin
22-Jun-01 3:20
suss22-Jun-01 3:20 
This is a *real* newbie question (Frown | :( , but how do you set the message box to be always on top. I want it to notify me from a program running in the background, but the message doesn't appear when the app is minimized.

thanks

GeneralRe: Always on top Pin
mloibl17-Jul-01 20:56
mloibl17-Jul-01 20:56 
QuestionAfxIsDescendant() ? Pin
Peter Sjöström28-Mar-01 5:58
Peter Sjöström28-Mar-01 5:58 
AnswerRe: AfxIsDescendant() ? Pin
mloibl28-Mar-01 20:10
mloibl28-Mar-01 20:10 
AnswerRe: AfxIsDescendant() ? Pin
Wes Jones17-Jul-01 13:19
Wes Jones17-Jul-01 13:19 
GeneralIcky global functions! Pin
Paul Wolfensberger17-Nov-00 4:15
Paul Wolfensberger17-Nov-00 4:15 
GeneralRe: Icky global functions! Pin
mloibl5-Feb-01 3:51
mloibl5-Feb-01 3:51 
GeneralRe: Icky global functions! Pin
mloibl5-Feb-01 9:13
mloibl5-Feb-01 9:13 
GeneralCrashes immediately on NT4 Pin
13-Nov-00 1:40
suss13-Nov-00 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.