HOWTO: How to Use PeekMessage() Correctly in Windows
来源:互联网 发布:spycall软件免费下载 编辑:程序博客网 时间:2024/06/06 18:40
HOWTO: How to Use PeekMessage() Correctly in Windows
SUMMARY
In the Windows environment, many applications use aPeekMessage() 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 aPeekMessage() 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 usePeekMessage() 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 toPeekMessage(). When an application is waiting for an input event, it should callGetMessage() or WaitMessage().
Remaining in a PeekMessage() loop when there is no background work causes system performance problems. A program in aPeekMessage() 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 thePeekMessage 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 aPeekMessage() 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 callingGetMessage() 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.
An application should not call PeekMessage() unless it has background processing to do between the calls toPeekMessage(). When an application is waiting for an input event, it should callGetMessage() or WaitMessage().
Remaining in a PeekMessage() loop when there is no background work causes system performance problems. A program in aPeekMessage() 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 thePeekMessage 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 aPeekMessage() 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 callingGetMessage() 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;
{
if (msg.message == WM_QUIT)
return TRUE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DispatchMessage(&msg);
}
BackgroundProcessing();
}
}
This loop can be rewritten in two ways, as shown below. Both of the followingPeekMessage() loops have two desirable properties:
Note that calls to functions such asIsDialogMessage() 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 thePostMessage function to inform the application that the device requires service. DLL functions can safely call thePostMessage() function because the PostMessage() function is reentrant.
•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();
}
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 thePostMessage function to inform the application that the device requires service. DLL functions can safely call thePostMessage() function because the PostMessage() function is reentrant.
- HOWTO: How to Use PeekMessage() Correctly in Windows
- HOWTO: How to Use PeekMessage() Correctly in Windows
- HOWTO: How to Use PeekMessage() Correctly in Windows
- How to Use PeekMessage() Correctly in Windows
- How to correctly use the extern keyword in C
- How to use Serial Port in Windows
- How To: Use Windows Authentication in ASP.NET 2.0
- How to use windows fonts in Linux WPS (kingsoft-office)
- How to Use Linux Commands in Windows with Cygwin
- How to Correctly and Uniformly Use Progress Monitors
- How to correctly set application badge value in iOS 8?
- How to use Pageheap.exe in Windows XP, Windows 2000, and Windows Server 2003
- How to use ntrights.exe tool to assign user right in windows server env
- How to Access and Use the Windows Hosts File in Windows 7 and Vista
- How To Use Macro In Program.
- How to use virt-install in OpenSolaris
- How to use tinyfck in drupal
- How to use Regular Expression in LotusScript
- ACM_1045_读取float类型,定义double类型
- 循环链表(java实现)
- EBS查询sql scripts
- JDBC中头文字N类型兼容方法笔记
- linux oracle netca命令
- HOWTO: How to Use PeekMessage() Correctly in Windows
- 用 LaTeX 排版编程技术书籍的一些个人经验
- ACM_1240_String和char转换
- OpenCV Mat类矩阵元素访问
- ACM进阶指南
- Tornado - Authentication - tornado.web.authenticated经典写法,带认真
- 找出两个含有相同元素个数的递增数列中第n小的数
- CollectionView导致内存泄露?
- 谈谈如何学习