Click here to Skip to main content
15,894,955 members
Articles / Desktop Programming / MFC
Article

OnIdle() implementation for CDialog based app´s

Rate me:
Please Sign up or sign in to vote.
4.58/5 (8 votes)
21 Jul 20021 min read 120.7K   1.3K   24   15
Simple implementation for the OnIdle function in the CDialog derived classes

Introduction

A very common and powerful programming tool for MFC applications is the idle time processing, which allows the programmer to be noticed when there aren't pending messages for the current thread. I was surprised when coding a dialog based application I found that this wonderful advantage was not available, due to the synchronous nature of the DoModal() method. Now the target is implementing the same functionality on a CDialog derived class.

The WM_IDLE loop trick

The code I made to solve this problem is really simple, it uses the GetQueueStatus function to check the message queue state. If there are not pending messages, it posts the WM_IDLE message to the current window, allowing it to call OnIdle(). Of course, all this code is implemented in an overridden version of WindowProc().

Emulating the behaviour of the lCount parameter (used to calculate how many idle time has passed since the last false return of OnIdle()) is self-explained in the following source code.

LRESULT CMyDialog::WindowProc(UINT message, 
                WPARAM wParam, LPARAM lParam)
{
    DWORD QueueStatus;
    LRESULT resValue = 0;
    bool OnIdleRetVal = true;

    if(message == WM_IDLE) {
        OnIdleRetVal = OnIdle((UINT)wParam);
        if(!OnIdleRetVal)
            wParam = 0;
    } else
        resValue = CDialog::WindowProc(message, 
        wParam, lParam);

    QueueStatus = GetQueueStatus(QS_ALLINPUT);

    if(HIWORD(QueueStatus) == 0)
        PostMessage(WM_IDLE, 
            wParam + (OnIdleRetVal ? 1 : 0), 0);

    return resValue;
}

Conclusion

Of course, OnIdle() should be declared and implemented in the class. The working mechanism, tips and tricks of the OnIdle function are explained on the CWinApp documentation part of the MSDN Library, so they will be not explained here for saving time and bytes ;).

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
Instructor/Trainer
Netherlands Netherlands
Emilio is a Computer Engineer currently working as software engineer in embedded systems.

Main interests are C/C++ programming, algorithmics, compilers, embedded systems, cryptography, and operating systems.

Comments and Discussions

 
GeneralMy vote of 5 Pin
JunfengGuo19-Oct-15 13:28
JunfengGuo19-Oct-15 13:28 
GeneralThanks, that was handy Pin
Yusuf X16-Jan-07 10:58
Yusuf X16-Jan-07 10:58 
GeneralUse CDialog's WM_TIMER Pin
Sanj Gunetileke9-Jun-05 7:26
Sanj Gunetileke9-Jun-05 7:26 
Generalenable and disable of this OnIdle trick during the execution of the program Pin
gehrti28-Apr-05 23:52
gehrti28-Apr-05 23:52 
GeneralLittle out of topic, but important Pin
ppppvvvv3-Sep-03 5:21
ppppvvvv3-Sep-03 5:21 
GeneralA slight issue Pin
Nish Nishant1-Aug-02 14:59
sitebuilderNish Nishant1-Aug-02 14:59 
GeneralRe: A slight issue Pin
2-Aug-02 9:11
suss2-Aug-02 9:11 
GeneralRe: A slight issue Pin
John Hind26-Oct-15 2:24
John Hind26-Oct-15 2:24 
QuestionWhat about WM_KICKIDLE? Pin
George23-Jul-02 16:47
George23-Jul-02 16:47 
AnswerRe: What about WM_KICKIDLE? Pin
Emilio Guijarro24-Jul-02 9:44
Emilio Guijarro24-Jul-02 9:44 
QuestionWhat about WM_IDLEUPDATECMDUI? Pin
Neville Franks22-Jul-02 11:24
Neville Franks22-Jul-02 11:24 
AnswerRe: What about WM_IDLEUPDATECMDUI? Pin
Emilio Guijarro23-Jul-02 7:33
Emilio Guijarro23-Jul-02 7:33 
GeneralRe: What about WM_IDLEUPDATECMDUI? Pin
Neville Franks23-Jul-02 15:09
Neville Franks23-Jul-02 15:09 
GeneralRe: What about WM_IDLEUPDATECMDUI? Pin
Emilio Guijarro24-Jul-02 9:42
Emilio Guijarro24-Jul-02 9:42 
AnswerRe: What about WM_IDLEUPDATECMDUI? Pin
Sheng Jiang 蒋晟22-Mar-04 18:20
Sheng Jiang 蒋晟22-Mar-04 18:20 

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.