Advertisement

MFC MessagePump

Started by July 08, 2000 06:55 PM
2 comments, last by Harvester 24 years, 5 months ago
I need some help... i have to perform some processing, and actually idle execution of an application for a while. However, i need the window message processing to keep on. With some pseydo code, something like this: while(halt_the_code_condition){ Process_messages_that_arrive } with VB or Delphi, this is pretty simple, as to calling a function. With MFC however i can''t locate any function that will allow this extra messages processing. Withought the usage of MFC, this is fairly easily accomplished. I''m sure they''ve written something on this subject. Any tips around? Thanks
... LEMMINGS ... LEMMINGS ... LEMMINGS ... LEM..... SpLaSh!...Could this be what we stand like before the mighty One?Are we LeMmIngS or WhAt!? ;)


There is a fairly undocumented member of CWinThread called PumpMessage that you can use.

Or, you can write your own using PeekMessage.

The discussion of PumpMessage is an article in the MSDN help with the title Idle Loop Processing.

You can also perform a search on "Idle Loop Processing" and turn up other information.

HTH!

Advertisement
Here''s another possibility. Overide the "OnIdle()" function in your CWinApp derived class.

This function is called whenever there are no messages in the message Queue. So whenever your app is idle, it will be called. You should ignore the first two calls, though, so MFC can do any house-cleaning. This is described in the online help, I am sure.

Here''s an implementation I wrote, so you can see how it might be done. Note that the return value is a BOOL ; return TRUE if you want OnIdle() to be called again, or FALSE if you don''t want it to be called again.. This only applies for the current ''idle'' state.. Once your app processes another message set and is idle (again) OnIdle() will be called (again).

    BOOL CMFCDD3App::OnIdle(LONG lCount) {	if(lCount < 2) {		CWinApp::OnIdle(lCount);		return TRUE;	}	if(m_bDrawActive && m_bAppActive)	{		// Update!		HRESULT ddrv = m_DDraw.Update();		// If Everything Okay, return TRUE		if(ddrv == DD_OK)			return TRUE;		// If we lost surfaces (Alt+TAB is a good way to do this..) try and restore		if(ddrv == DDERR_SURFACELOST)		{			// Wait if window is minimized..			if(m_pMainWnd->IsIconic())				return TRUE;			// Try and restore..			if(m_DDraw.Restore() == TRUE)				return TRUE;		}		//		// Something went bad.. we have to abort..		//#ifdef _DEBUG		TraceDDError(ddrv);#endif		ReleaseDirectDraw();		return FALSE;	}	return FALSE;}    


Hope that helps..

// CHRIS
// CHRIS [win32mfc]
Maybe I should make it apply more directly to your question. If you have a condition, call it ''HALT_CONDITION_MET()'' returning TRUE or FALSE, you might implement the OnIdle() like this:

    BOOL CYourApp::OnIdle(LONG lCount) {	if(lCount < 2) {		CWinApp::OnIdle(lCount);		return TRUE;	}	if(HALT_CONDITION_MET())	{		// Do Whatever you need to.. post a message or ...		PostQuitMessage(0); // Terminate Program..	}		// Returning TRUE will cause OnIdle() to be called	// repeatedly -- and the CPU meter will max out since	// this will get called again and again.  Return TRUE	// only if your HALT_CONDITION_MET() might return TRUE	// while your app is idle.  Otherwise return FALSE; so	// this function is called only once after each time	// your app is idle.		return(FALSE);}    
// CHRIS [win32mfc]

This topic is closed to new replies.

Advertisement