MFC如何建立CPU空闲处理(OnIdle )

简介: about OnIdle  MFC Library Reference  CWinApp::OnIdle    Override this member function to perform idle-time processing.
about OnIdle 
MFC Library Reference 
CWinApp::OnIdle 

 

Override this member function to perform idle-time processing.

virtual BOOL OnIdle(
   LONG lCount
);

Parameters

lCount

A counter incremented each time OnIdle is called when the application's message queue is empty. This count is reset to 0 each time a new message is processed. You can use the lCount parameter to determine the relative length of time the application has been idle without processing a message.

Nonzero to receive more idle processing time; 0 if no more idle time is needed.

OnIdle is called in the default message loop when the application's message queue 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:

  1. 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.

  2. OnIdle performs some processing and returns a nonzero value to indicate it should be called again to do further processing.

  3. The message loop checks the message queue again. If no messages are pending, it calls OnIdle again, incrementing the lCount argument.

  4. 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 0.

Do not perform lengthy tasks during OnIdle because your application cannot process user input until OnIdle returns.

NoteNote

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 CWinThreadIsIdleMessage. 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.

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
}
// 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在CWinThread里面. 虚函数.

 CWinApp派生自CWinThread.
在App类里面直接写个
virtual BOOL OnIdle(LONG lCount);

目录
相关文章
|
Unix Linux Apache
离线安装Superset 0.37(截图详细版)
上文提到了Superset 0.37的在线安装方式,只需要更新pip,然后pip install就可以了。但是在生产环境中,特别是内网环境中,很多时候是没有外网的,这时候就需要采取离线安装的方式。 本文将详细介绍在Linux系统中离线安装Superset的全过程,并整理了安装过程中遇到的错误。
1233 0
离线安装Superset 0.37(截图详细版)
|
监控 安全 开发者
【Qt 并发 】理解Qt中事件循环与并发机制的协同工作
【Qt 并发 】理解Qt中事件循环与并发机制的协同工作
1352 4
|
JavaScript 前端开发 IDE
QCAD v3.23.0.2源码编译,使用VS2017+Qt5.12.5环境
QCAD v3.23.0.2源码编译,使用VS2017+Qt5.12.5环境
1319 0
|
4月前
|
算法 API 开发者
沪深A股实时行情API接入指南
逐笔Tick数据接口,查询A股上市公司的最新成交明细,确保获取市场的最新交易信息。返回示例如下:
|
12月前
|
前端开发 API UED
React 按需加载 Lazy Loading
随着 Web 应用复杂度增加,页面加载速度成为影响用户体验的关键因素。React 提供了按需加载(Lazy Loading)功能,通过 `React.lazy` 和 `Suspense` 实现动态加载组件,减少初始加载时间,提升性能。本文从基础概念入手,探讨常见问题、易错点及解决方案,并通过代码示例详细说明。
502 1
|
6月前
|
网络协议 算法 安全
TCP协议(三次握手、流量控制、拥塞控制)
TCP协议是一种可靠的传输层通信协议,通过三次握手建立连接,确保数据安全传输。流量控制通过接收窗口避免接收方缓冲区溢出,拥塞控制则利用拥塞窗口调节网络传输速度,防止网络拥堵。三者协同工作,保障TCP在复杂网络环境中实现高效、可靠的数据传输。
2005 11
|
搜索推荐 数据挖掘 API
探讨京东商品 API 接口:运用及收益
在数字化商业时代,京东商品 API 接口为企业和开发者提供了丰富的数据资源和应用机会。通过获取商品信息、价格、库存、用户评价等数据,API 接口助力电商平台建设、数据分析、移动应用开发和营销推广,提升商业价值、优化用户体验,并支持战略决策。遵守规范、保障数据安全是实现长期发展的关键。
287 2
|
Java API
Java 8新特性之Stream API详解
【5月更文挑战第30天】本文将详细介绍Java 8中的一个重要新特性——Stream API。Stream API是Java 8中引入的一种新的数据处理方式,它允许我们以声明式的方式处理数据,使得代码更加简洁、易读。文章将从Stream的基本概念、创建方式、常用操作以及实战案例等方面进行详细讲解,帮助读者深入理解并掌握Stream API的使用。
223 2
|
存储 缓存 编译器
Linux源码阅读笔记06-RCU机制和内存优化屏障
Linux源码阅读笔记06-RCU机制和内存优化屏障
|
机器学习/深度学习 人工智能 PyTorch
PyTorch快速入门与深度学习模型训练
这篇文章是PyTorch的入门指南,介绍了PyTorch作为深度学习框架的基本概念和使用方法。内容包括PyTorch的背景、基础操作如张量创建、运算、自动微分,以及如何构建和训练简单的全连接神经网络模型进行MNIST手写数字识别。通过这篇文章,读者可以快速了解如何在PyTorch中搭建和训练深度学习模型。
699 4