Click here to Skip to main content
13,344,765 members (50,407 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 17 Mar 2010

Simulating CWinApp::OnIdle in C

, 18 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Learn how to create your CWinApp::OnIdle in C

MFC allows you to override CWinApp::OnIdle function to perform idle-time processing.

This function is called whenever there're no messages waiting for processing in the message queue.

In this function, you can perform some secondary processing like updating the status bar, toolbar, etc.

The definition of this function is as follows:

virtual BOOL OnIdle(LONG lCount);

If you are interested, you can override this function and do some processing. The following example paints a random rectangle while the application is idle (that is no processing is carried on). Thus, this code doesn't make the application irresponsive.

BOOL OnIdle(LONG lCount)

	CClientDC dc(m_pMainWnd);
	RECT rct;

		rand() % rct.right,
		rand() % rct.bottom,
		rand() % rct.right,
		rand() % rct.bottom);

	return TRUE;

This function receives only a single argument, lCount; it contains a value incremented each time OnIdle is called, and it is reset to 0 each time a new session is established. A new session is established each time your application finishes processing pending messages and no messages are left.

MFC continues to call CWinApp::OnIdle (incrementing lCount each time) as long as there’re no messages waiting for processing. If the application received a new message, MFC ends the current session and stops calling OnIdle until it establishes a new session again. If you want, you can return TRUE to indicate that further processing is required and MFC should call OnIdle again (as long as we are in the current session,) or FALSE to indicate that processing has been finished and MFC should not call OnIdle again until a new session is established.

Notice that, you should call the base implementation of CWinApp::OnIdle because the default implementation of OnIdle updates command UI objects like menu items and toolbars besides doing some internal structure cleanup.

Unfortunately, C doesn't include OnIdle function. However, we could work out ours.

The following C example shows our new handmade message queue that simulates CWinApp::OnIdle:

while (TRUE) {
	if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {

		// if a quit message
		if (WM_QUIT == msg.message)
			break; // exit the loop

		// process other messages
	else {	// no messages are waiting
		// do some idle processing

The key function is PeekMessage. This function checks the message queue and if a message was found, it removes the message from the queue (if PM_REMOVE specified,) initializes the MSG object with message data, and returns TRUE to the caller. If no message was found, PeekMessage returns FALSE thus executing the code (secondary processing) in the else statement.

You can also create your fully-featured handmade OnIdle. Consider the following code:

BOOL OnIdle(LONG lCount);

int WINAPI WinMain(. . .)
	MSG msg;
	LONG lCount;

	. . .

	while (TRUE) {
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
			// found a message, resetting
			lCount = 0;

			if (WM_QUIT == msg.message)

		else {
			if (lCount >= 0) {
				if (!OnIdle(lCount)) {
					// set a flag indicates
					// no further OnIdle
					// calls till a new
					// session
					lCount = -1;

				// increment the counter

	return msg.wParam;

BOOL OnIdle(LONG lCount)

	do some processing


	return FALSE;

Have a nice day!

Filed under: Windows Management Tagged: C, CodeProject, CPP, MFC, Windows Management


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

Mohammad Elsheimy
Technical Writer Just Like a Magic
Egypt Egypt
Independent technical writer from Egypt born in 1991.

I am a student of The Holy Quranic Sciences Institute. There, we study the Holy Quranic sciences and Islamic legislation.

I currently run two sites:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 18 Mar 2010
Article Copyright 2010 by Mohammad Elsheimy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid