OnIdle的处理

转载 2004年08月23日 18:48:00

Remarks
Override this method to perform idle-time processing. OnIdle is called in the default message loop when the message queue in the application is empty. Use your override to call your own background idle-handler tasks.

OnIdle should return 0 to indicate that no idle processing time is required. The lCount parameter is incremented each time OnIdle is called when the message queue is empty and resets to 0 each time a new message is processed. You can call your different idle routines based on this count.

The following summarizes idle loop processing:

If the message loop in the Microsoft Foundation Class Library checks the message queue and finds no pending messages, it calls OnIdle for the application object and supplies 0 as the lCount argument.
OnIdle performs some processing and returns a nonzero value to indicate it should be called again to do further processing.
The message loop checks the message queue again. If no messages are pending, it calls OnIdle again, incrementing the lCount argument.
Eventually, OnIdle finishes processing all its idle tasks and returns 0. This tells the message loop to stop calling OnIdle until the next message is received from the message queue, at which point the idle cycle restarts with the argument set to zero.
Do not perform lengthy tasks during OnIdle because your application cannot process user input until OnIdle returns.

The default implementation of OnIdle updates command user-interface objects such as menu items and toolbar buttons, and it performs internal data structure cleanup. Therefore, if you override OnIdle, you must call CWinApp::OnIdle with the lCount in your overridden version. First call all base-class idle processing, that is, until the base class OnIdle returns 0. If you need to perform work before the base-class processing completes, review the base-class implementation to select the proper lCount during which to do your work.

If you do not want OnIdle to be called whenever a message is retrieved from the message queue, you can override the CWinThread::IsIdleMessage. If an application has set a very short timer, or if the system is sending the WM_SYSTIMER message, then OnIdle will be called repeatedly, and degrade performance.

Example
The following two examples show how to use OnIdle. The first example processes two idle tasks using the lCount argument to prioritize the tasks. The first task is high priority, and you should do it whenever possible. The second task is less important and should be done only when there is a long pause in user input. Note the call to the base-class version of OnIdle. The second example manages a group of idle tasks with different priorities.

BOOL CMyApp::OnIdle(LONG lCount)
{
    BOOL bMore = CWinApp::OnIdle(lCount);

    if (lCount == 0)
    {
    TRACE("App idle for short period of time/n");
    bMore = TRUE;
    }
    else if (lCount == 10)
    {
    TRACE("App idle for longer amount of time/n");
        bMore = TRUE;
    }
    else if (lCount == 100)
    {
        TRACE("App idle for even longer amount of time/n");
        bMore = TRUE;
    }
    else if (lCount == 1000)
    {
        TRACE("App idle for quite a long period of time/n");
     // bMore is not set to TRUE, no longer need idle.
     // IMPORTANT: bMore is not set to FALSE since CWinApp::OnIdle may
     // have more idle tasks to complete.
    }

    return bMore;
     // Return TRUE as long as there is any more idle tasks.
}

Second Example
// In this example, four idle loop tasks are given various
// opportunities to run:
// Task1 is always given a chance to run during idle time, provided
//   that no message has queued up while the framework was processing
//   its own idle loop tasks (at lCount levels 0 and 1).
// Task2 is given a chance to run only if Task1 has already run,
//   provided that no message has queued up while Task1 was running.
// Task3 and Task4 are given a chance to run only if both Task1 and
//   Task2 have already run, and no message has queued up in the mean
//   time.  If Task3 gets its chance to run, then Task4 always gets
//   a chance to run immediately after Task3.

BOOL CMyApp::OnIdle(LONG lCount)
{
   // In this example, as in most applications, you should let the
   // base class CWinApp::OnIdle complete its processing before you
   // attempt any additional idle loop processing.
   if (CWinApp::OnIdle(lCount))
      return TRUE;  

   // The base class CWinApp::OnIdle reserves the lCount values 0
   // and 1 for the framework's own idle processing.   If you wish to
   // share idle processing time at a peer level with the framework,
   // then replace the above if-statement with a straight call to
   // CWinApp::OnIdle; and then add a case statement for lCount value
   // 0 and/or 1. Study the base class implementation first to
   // understand how your idle loop tasks will compete with the
   // framework's idle loop processing.

   switch (lCount)
   {
      case 2:
         Task1();
         return TRUE; // Next time give Task2 a chance.
      case 3:
         Task2();
         return TRUE; // Next time give Task3 and Task4 a chance.
      case 4:
         Task3();
         Task4();
         return FALSE; // Cycle through the idle loop tasks again.
   }
   return FALSE;
}

OnIdle:MFC空闲处理

当没有任何 Windows 消息正在被处理时,框架调用 CWinApp 的成员函数 OnIdle(如“MFC 库参考”中所述)。重写 OnIdle 以执行后台任务。默认版本更新用户界面对象(如工具栏按...
  • up5p5mm
  • up5p5mm
  • 2008-06-13 23:33:00
  • 3239

MFC OnIdle函数学习

先看下MSDN对OnIdle()介绍:CWinApp::OnIdleOnIdle is called in the default message loop when the application’...
  • smallmount123
  • smallmount123
  • 2016-04-06 22:58:22
  • 396

OnIdle的详细说明

CWinApp::OnIdlevirtual BOOL OnIdle( LONG lCount );返回值:如果要接收更多的空闲处理时间,则返回非零值;如果不需要更多的空闲时间则返回0。参数:lCou...
  • tsing_best
  • tsing_best
  • 2014-05-05 15:46:54
  • 551

OnIdle详解

CWinApp::OnIdlevirtual BOOL OnIdle( LONG lCount );返回值:如果要接收更多的空闲处理时间,则返回非零值;如果不需要更多的空闲时间则返回0。参数: lCo...
  • zhouzhihui1988
  • zhouzhihui1988
  • 2009-08-11 17:56:00
  • 342

空闲时间的处理:OnIdle,以消息循环过程中为例(顺便解释PeekMessage与GetMessage的不同)

所谓空闲时间(idle time),是指「系统中没有任何消息等待处理」的时间。举个例子, 没有任何程序使用定时器(timer,它会定时送来WM_TIMER),使用者也没有碰触键盘 和鼠标或任何外围...
  • ylwdi
  • ylwdi
  • 2013-04-25 21:14:29
  • 643

MFC里的OnIdle()

CWinThread::Run是程序生命的"活水源头"(侯捷:《深入浅出MFC》,函数存在于VC++ 6.0安装目录下提供的THRDCORE.CPP文件中): // main running rout...
  • hell2pradise
  • hell2pradise
  • 2009-07-08 10:25:00
  • 2515

操作IIS

当应用程序进行复杂计算或占用很多系统资源的操作时,用户点击程序界面按钮时无法响应,有两种解决方法:计算线程,消息循环重载技术,即在应用程序中处理Windows消息循环。这样既可以在主线程中进行复杂计算...
  • todaywlq
  • todaywlq
  • 2007-04-30 11:01:00
  • 430

OnIdle的调用

OnIdle的调用 首先看GetMessage(PumpMessage中被调用)与PeekMessage的区别: 当消息队列为空时,程序会被阻塞在GetMessage的调用处。这时系统就有会去照顾...
  • tms_li
  • tms_li
  • 2011-01-17 23:24:00
  • 2644

MFC OnIdle的处理

CWinThread::Run()的代码如下: // main running routine until threadexits intCWinThread::Run() {       ASSER...
  • jiangdewei2012
  • jiangdewei2012
  • 2014-03-21 13:38:14
  • 305

OnCmdMsg -- CFrameWnd::OnCmdMsg and CView::OnCmdMsg

///////////////////////////////////////////////////////////////////////////// // CFrameWnd command/...
  • wishfly
  • wishfly
  • 2013-03-15 11:09:38
  • 691
收藏助手
不良信息举报
您举报文章:OnIdle的处理
举报原因:
原因补充:

(最多只允许输入30个字)