CWinApp::OnIdle

覆寫這個成員函式執行閒置時間處理。

virtual BOOL OnIdle(
   LONG lCount 
);

參數

  • lCount
    已加入的計數器,每次 OnIdle 呼叫,以便將應用程式的訊息佇列是空的。每次新訊息處理常式,這個計數器會重設為 0。您可以使用 lCount 參數判斷應用程式處於閒置狀態,而不需處理訊息的相對時間。

傳回值

非零接收更閒置處理時間,0,如果沒有其他閒置時間是不需要的。

備註

指出應用程式的訊息佇列是空的時,OnIdle 在預設訊息迴圈呼叫。使用您的覆寫會呼叫您的背景閒置處理序工作。

OnIdle 應傳回 0 表示不需要處理時間延遲。lCount 參數會加入 OnIdle ,每次呼叫時,訊息佇列是空的且重設為 0 時新訊息每次處理。您可以用自己的 Variant 閒置處理常式根據這個計數。

下列摘錄閒置迴圈 (Loop) 處理:

  1. 如果在 MFC 程式庫的訊息迴圈檢查訊息佇列並不會發現擱置訊息,它會呼叫應用程式物件的 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 類別

階層架構圖