CWinApp::OnIdle

重写该成员函数执行空闲时间处理。

virtual BOOL OnIdle( 
   LONG lCount  
);

参数

  • lCount
    递增计数器,每次 OnIdle 调用,当应用程序的消息队列为空。 每次新的消息处理,此计数重新设置为0。 可以使用 lCount 参数确定应用程序是空闲,不处理消息的相对时间长度。

返回值

非零接收更空闲的处理时间;0,如果没有其他空闲时间不是必需的。

备注

当应用程序的消息队列为空时,OnIdle 在默认消息循环调用。 使用重写中调用您的背景空闲处理程序任务。

OnIdle 应返回0指示不需要任何空闲处理的时间。 lCount 参数增加,每次 OnIdle 调用,当消息队列为空时,并重置为0新的消息每次处理。 您可以按照您不同的空闲实例基于此计数。

下面总结空闲循环处理:

  1. 如果消息循环Microsoft基础选件类库中检查消息队列并没有找到挂起的消息,则调用应用程序对象的 OnIdle 并提供0作为 lCount 参数。

  2. OnIdle 执行某些处理并返回非零值指示应再次调用它执行进一步处理。

  3. 消息循环检测再次消息队列。 如果消息不挂起,然后再次调用 OnIdle,增加 lCount 参数。

  4. 最后,OnIdle 处理完所有其空闲任务并返回0。 此通知消息循环停止调用 OnIdle,直到下将从消息队列收到,此时空闲循环重新启动将参数设置为0。

不要执行长期任务在 OnIdle 期间,因为应用程序无法处理用户输入,直到 OnIdle 返回。

备注

OnIdle 的默认实现更新命令用户界面对象(如菜单项和工具栏按钮,因此,它执行内部数据结构清除。因此,因此,如果重写 OnIdle,必须调用 CWinApp::OnIdle 以及您的重写版本的 lCount。第一次调用任何基类闲置处理(也就是说,直到基类 OnIdle 返回0)。如果需要对工作,在基类处理完成之前,请查看一个基类的实现选择过程完成工作的相应 lCount。

如果不希望 OnIdle 调用时,将从消息队列中检索,可以重写 CWinThreadIsIdleMessage。 如果应用程序设置非常短路计时器,或者,如果系统发送 WM_SYSTIMER 信息,然后 OnIdle 将重复调用,并降低性能。

示例

下面两个示例演示如何使用 OnIdle。 第一个示例处理两空闲的任务使用 lCount 参数优先考虑任务。 第一个任务是高优先级的,因此,您应执行此操作尽可能。 仅当在用户进入时,的长暂停第二个任务是不太重要的,因此执行。 请注意对 OnIdle的基类版本。 第二个示例管理空闲任务的一组不同的优先级。

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

   if (lCount == 0)
   {
      TRACE(_T("App idle for short period of time\n"));
      bMore = TRUE;
   }
   else if (lCount == 10)
   {
      TRACE(_T("App idle for longer amount of time\n"));
      bMore = TRUE;
   }
   else if (lCount == 100)
   {
      TRACE(_T("App idle for even longer amount of time\n"));
      bMore = TRUE;
   }
   else if (lCount == 1000)
   {
      TRACE(_T("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 are any more idle tasks
}

第二个示例

// 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 CMyWinApp::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 TRUE;
}

要求

Header: afxwin.h

请参见

参考

CWinApp Class

层次结构图