Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Simulating CWinApp::OnIdle in C

5.00/5 (2 votes)
18 Mar 2010CPL2 min read 1  
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:

C++
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.

C++
BOOL OnIdle(LONG lCount)
{
	CWinApp::OnIdle(lCount);

	CClientDC dc(m_pMainWnd);
	RECT rct;

	m_pMainWnd->GetClientRect(&rct);
	SetRect(&rct,
		rand() % rct.right,
		rand() % rct.bottom,
		rand() % rct.right,
		rand() % rct.bottom);

	dc.Rectangle(&rct);
	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:

C#
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
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	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:

C++
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)
				break;

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

				// increment the counter
				lCount++;
			}
		}
	}

	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

License

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