Click here to Skip to main content
6,822,613 members and growing! (20,440 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Message Handling     Intermediate

OnIdle() implementation for CDialog based app´s

By Emilio Guijarro

Simple implementation for the OnIdle function in the CDialog derived classes
VC6Win2K, WinXP, MFC, Dev
Posted:21 Jul 2002
Views:72,067
Bookmarked:20 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.00 Rating: 3.84 out of 5

1

2

3
2 votes, 28.6%
4
5 votes, 71.4%
5

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

About the Author

Emilio Guijarro


Member
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.
Occupation: Instructor/Trainer
Location: Netherlands Netherlands

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralThanks, that was handy PinmemberYusuf X11:58 16 Jan '07  
GeneralUse CDialog's WM_TIMER PinsussSanj Gunetileke8:26 9 Jun '05  
Generalenable and disable of this OnIdle trick during the execution of the program Pinmembergehrti0:52 29 Apr '05  
GeneralLittle out of topic, but important Pinmemberppppvvvv6:21 3 Sep '03  
GeneralA slight issue PineditorNishant S15:59 1 Aug '02  
GeneralRe: A slight issue Pinsuss10:11 2 Aug '02  
GeneralWhat about WM_KICKIDLE? PinmemberGeorge17:47 23 Jul '02  
GeneralRe: What about WM_KICKIDLE? PinmemberEmilio Guijarro10:44 24 Jul '02  
GeneralWhat about WM_IDLEUPDATECMDUI? PinmemberNeville Franks12:24 22 Jul '02  
GeneralRe: What about WM_IDLEUPDATECMDUI? PinmemberEmilio Guijarro8:33 23 Jul '02  
GeneralRe: What about WM_IDLEUPDATECMDUI? PinmemberNeville Franks16:09 23 Jul '02  
GeneralRe: What about WM_IDLEUPDATECMDUI? PinmemberEmilio Guijarro10:42 24 Jul '02  
GeneralRe: What about WM_IDLEUPDATECMDUI? PinmemberJiang Sheng19:20 22 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jul 2002
Editor: Chris Maunder
Copyright 2002 by Emilio Guijarro
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project