The second way to address the CPU usage of the delay function in the previous section (up to 50%) is to use the message mechanism to wait for messages or timeouts through API functions MsgWaitForMultipleObjects, thus avoiding the use of cyclic detection to make CPU usage too high. The complete modified version of the delay function code is as follows:
[Delphi]View Plaincopyprint?
- Procedure Delay (Dwmilliseconds:dword);
- Var
- Endtick:dword;
- Event:thandle;
- Begin
- Timer1. Enabled:=false;
- Event: = CreateEvent (nil,false,false,nil);
- Try
- Endtick: = Gettickcount+dwmilliseconds;
- while (Dwmilliseconds > 0) and
- (MsgWaitForMultipleObjects (1, Event, False,
- Dwmilliseconds, qs_allinput) = wait_object_0+1) do
- Begin //is also in the delay period and there is an input message to execute the following distribution message
- Application. ProcessMessages;
- Dwmilliseconds: = Endtick-gettickcount;
- End
- Finally
- CloseHandle (Event); //Close event handle to destroy event object
- End
- Timer1. Enabled:=true;
- End
- {Two API functions used}
- //***********************************************************************
- Function: CreateEvent//Create Event object
- Parameters: Lpeventattributes=nil//default security character
- Bmanualreset=false//Auto-Restore: When an event is released by a waiting thread,
- The system will automatically revert the event state to a no-signal state
- Binitialstate=false//Specifies that the initial state of the event object is no signal state
- Lpname=nil//Nameless objects
- Return value: Event object handle
- //***********************************************************************
- //**************************************************************************
- Function: msgwaitformultipleobjects//wait until return condition is satisfied returns immediately
- //
- Return condition: ① one or All (the third parameter) object in the specified (signal) event object (the second parameter) sends a signal
- (The wait/timeout time specified by either ② (fourth parameter) has been reached
- Satisfied) ③ The specified message (the fifth parameter) has reached the input queue of the thread
- //
- Parameters: Ncount=1 The number of handles in the specified list is 1
- Phandles=event the first element in the specified object handle combination is an Event
- Fwaitall=false any object to send a signal
- Dwmilliseconds=dwmilliseconds the number of milliseconds to wait is the delay time
- Dwwakemask=qs_allinput identifies a specific message type as a message queue for any message
- return value: Wait_object_0+1 (ncount) with the specified type of message arrives
- //*************************************************************************
The following is a description of MsgWaitForMultipleObjects on MSDN:
The msgwaitformultipleobjects function Determines whether the wait criteria has been met. If the criteria has not been met, the calling thread enters a efficient wait state, using very little processor Time WHI Le waiting for the conditions of the the wait criteria to be met.
MsgWaitForMultipleObjects determines whether the wait condition (that is, the return condition) is met, and if not, the calling thread (the main thread in this case) enters an efficient wait state: Use very little CPU time to wait for the return condition to be established.
For more information about msgwaitformultipleobjects, see http://msdn.microsoft.com/zh-cn/library/ms931460
Test
Using the above mentioned three functions: Sleep, delay, improved delay, a simple test, the results are as follows:
(1) using sleep
(2) using delay
(3) using the modified version of delay
Analysis Summary:
The ①sleep function suspends the program so that neither the interface nor the timer is responsive, and the CPU is not consumed.
The ②delay function uses the cyclic detection method, although the interface can be timely response, but in the delay time of 5 seconds CPU occupancy rate reached 50%.
The ③ improved delay function is a responsive interface with low CPU usage (almost 0, only 1% when a given input message arrives).
④ the latter two functions do not respond in a timely manner to a form close message.
http://blog.csdn.net/tht2009/article/details/6685622
Improved delay function delay (using msgwaitformultipleobjects to wait for messages or timeouts)