• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            山寨:不是最好的,是最適合我們的!歡迎體驗山寨 中文版MSDN

            Blog @ Blog

            當(dāng)華美的葉片落盡,生命的脈絡(luò)才歷歷可見。 -- 聶魯達

            常用鏈接

            統(tǒng)計

            積分與排名

            BBS

            Blog

            Web

            最新評論

            如何正確的使用PeekMessage()

            HOWTO: How to Use PeekMessage() Correctly in Windows

            SUMMARY

            In the Windows environment, many applications use a PeekMessage() loop to perform background processing. Such applications must allow the Windows system to enter an idle state when their background processing is complete. Otherwise, system performance, "idle-time" system processes such as paging optimizations, and power management on battery-powered systems will be adversely affected.

            While an application is in a PeekMessage() loop, the Windows system cannot go idle. Therefore, an application should not remain in a PeekMessage() loop after its background processing has completed.

            NOTE: The PeekMessage method described in this article is only needed if your application is a 32-bit application for Windows and is, for some reason, unable to create threads and perform background processing.

            MORE INFORMATION

            Many Windows-based applications use PeekMessage() to retrieve messages while they are in the middle of a long process, such as printing, repaginating, or recalculating, that must be done "in the background." PeekMessage() is used in these situations because, unlike GetMessage(), it does not wait for a message to be placed in the queue before it returns.

            An application should not call PeekMessage() unless it has background processing to do between the calls to PeekMessage(). When an application is waiting for an input event, it should call GetMessage() or WaitMessage().

            Remaining in a PeekMessage() loop when there is no background work causes system performance problems. A program in a PeekMessage() loop continues to be rescheduled by the Windows scheduler, consuming CPU time and taking time away from other processes.

            In enhanced mode, the Virtual Machine (VM) in which Windows is running will not appear to be idle as long as an application is calling the PeekMessage function. Therefore, the Windows VM will continue to receive a considerable fraction of CPU time.

            Many power management methods employed on laptop and notebook computers are based on the system going idle when there is no processing to do. An application that remains in a PeekMessage() loop will make the system appear busy to power management software, resulting in excessive power consumption and shortening the time that the user can run the system.

            In the future, the Windows system will make more and more use of idle time to do background processing, which is designed to optimize system performance. Applications that do not allow the system to go idle will adversely affect the performance of these techniques.

            All these problems can be avoided by calling the PeekMessage() function only when there is background work to do, and calling GetMessage() or WaitMessage() when there is no background work to do.

            For example, consider the following PeekMessage() loop. If there is no background processing to do, this loop will continue to run without waiting for messages, preventing the system from going idle and causing the negative effects described above.
               // This PeekMessage loop will NOT let the system go idle.
               for( ;; )
               {
                  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                  {
                     if (msg.message == WM_QUIT)
                        return TRUE;
                     TranslateMessage(&msg);
                     DispatchMessage(&msg);
                  }
                  BackgroundProcessing();
                }
            This loop can be rewritten in two ways, as shown below. Both of the following PeekMessage() loops have two desirable properties:

            They process all input messages before performing background processing, providing good response to user input.
            The application "idles" (waits for an input message) when no background processing needs to be done.
             

            Improved PeekMessage Loop 1

               // Improved PeekMessage() loop
               for(;;)
               {
                  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                  {
                     if (msg.message == WM_QUIT)
                        return TRUE;
                     TranslateMessage(&msg);
                     DispatchMessage(&msg);
                  }
                  if (IfBackgroundProcessingRequired())
                     BackgroundProcessing();
                  else
                     WaitMessage(); // Will not return until a message is posted.
               }	

            Improved PeekMessage Loop 2

               // Another improved PeekMessage() loop
               for (;;)
               {
                  for (;;)
                  {
                     if (IfBackgroundProcessingRequired())
                     {
                        if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                           break;
                     }
                     else
                        GetMessage(&msg, NULL, 0, 0, 0);
                     if (msg.message == WM_QUIT)
                        return TRUE;
                     TranslateMessage(&msg);
                     DispatchMessage(&msg);
                  }
                  BackgroundProcessing();
               }		
            Note that calls to functions such as IsDialogMessage() and TranslateAccelerator() can be added to these loops as appropriate.

            There is one case in which the loops above need additional support: if the application waits for input from a device (for example, a fax board) that does not send standard Windows messages. For the reasons outlined above, a Windows-based application should not use a PeekMessage() loop to continuously poll the device. Rather, implement an Interrupt Service Routine (ISR) in a Dynamic-Link Library (DLL). When the ISR is called, the DLL can use the PostMessage function to inform the application that the device requires service. DLL functions can safely call the PostMessage() function because the PostMessage() function is reentrant.

            posted on 2008-01-19 16:12 isabc 閱讀(2906) 評論(0)  編輯 收藏 引用 所屬分類: VC Function

            廣告信息(免費廣告聯(lián)系)

            中文版MSDN:
            歡迎體驗

            精品久久久无码中文字幕| 中文成人无码精品久久久不卡 | 国产99久久精品一区二区| 亚洲va国产va天堂va久久| 婷婷久久香蕉五月综合加勒比| 青青草原精品99久久精品66| 久久免费视频观看| 女同久久| 亚洲狠狠久久综合一区77777 | 久久99久国产麻精品66| 亚洲中文精品久久久久久不卡| 亚洲国产精品久久| 亚洲国产精品无码久久九九| 国产V亚洲V天堂无码久久久| 久久久久亚洲AV片无码下载蜜桃 | 国产成人精品久久综合 | 日韩精品无码久久久久久| 国产精品一区二区久久精品无码| 伊人色综合九久久天天蜜桃| 色综合久久中文色婷婷| 日本强好片久久久久久AAA| 亚洲精品国产自在久久| 国产三级观看久久| 国产一久久香蕉国产线看观看| 一本色道久久综合狠狠躁| 国产99久久久久久免费看| 国内精品久久人妻互换 | 欧美777精品久久久久网| 亚洲AV无码久久寂寞少妇| 国色天香久久久久久久小说 | 久久亚洲国产成人精品无码区| 精品久久久久久久无码| 久久国产免费直播| 18禁黄久久久AAA片| 亚洲欧洲精品成人久久奇米网| 久久青草国产精品一区| 91精品国产高清久久久久久io| 久久亚洲精品无码AV红樱桃| 无码久久精品国产亚洲Av影片| 无码日韩人妻精品久久蜜桃 | 久久久亚洲欧洲日产国码二区|