How to utilize the application's spare time?

来源:互联网 发布:87版红楼梦 知乎 编辑:程序博客网 时间:2024/05/08 06:14

 A window application will enter the idle time if there is no message waiting for processing. So how to utilize this interval time to do opearions.

Generally, a window message loop is like this:


while(GetMessagee(&msg,NULL,NULL,NULL)
{
   TranslateMessage(
&
msg);
   DispatchMessage(
&
msg);
}

If we didn't receive any system message, we can use this "spare time" of our application to do some background processing and even do some stuff. This process is called Idle Processing. We need to insert our message loop right after the initialization of our global variables.

while( TRUE )
{
    MSG msg;

    
if( PeekMessage( &msg, NULL, 00
, PM_REMOVE ) )
    
{
        
// Check for a quit message

        if( msg.message == WM_QUIT )
            
break
;

        TranslateMessage( 
&
msg );
        DispatchMessage( 
&
msg );
    }

    
else
    
{
        ProcessIdle();
    }

}
In our message loop, the first thing we do is check the message queue for messages to our application. This is accomplished by calling the PeekMessage function. If the function returns true, we call TranslateMessage and DispatchMessage so that the messages received by our program are processed. If we have no message, we'll call another function called ProcessIdle.In our message loop, the first thing we do is check the message queue for messages to our application. This is accomplished by calling the PeekMessage function. If the function returns true, we call TranslateMessage and DispatchMessage so that the messages received by our program are processed. If we have no message, we'll call another function called ProcessIdle.
原创粉丝点击