Improved delay function delay (with msgwaitformultipleobjects waiting for messages or timeouts)

Source: Internet
Author: User
Tags sleep function

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?
  1. Procedure Delay (Dwmilliseconds:dword);
  2. Var
  3. Endtick:dword;
  4. Event:thandle;
  5. Begin
  6. Timer1.  Enabled:=false;
  7. Event: = CreateEvent (nil,false,false,nil);
  8. Try
  9. Endtick: = Gettickcount+dwmilliseconds;
  10. while (Dwmilliseconds > 0) and
  11. (MsgWaitForMultipleObjects (1, Event, False,
  12. Dwmilliseconds, qs_allinput) = wait_object_0+1) do
  13. Begin //is also in the delay period and there is an input message to execute the following distribution message
  14. Application.  ProcessMessages;
  15. Dwmilliseconds: = Endtick-gettickcount;
  16. End
  17. Finally
  18. CloseHandle (Event); //Close event handle to destroy event object
  19. End
  20. Timer1.  Enabled:=true;
  21. End
  22. {Two API functions used}
  23. //***********************************************************************
  24. Function: CreateEvent//Create Event object
  25. Parameters: Lpeventattributes=nil//default security character
  26. Bmanualreset=false//Auto-Restore: When an event is released by a waiting thread,
  27. The system will automatically revert the event state to a no-signal state
  28. Binitialstate=false//Specifies that the initial state of the event object is no signal state
  29. Lpname=nil//Nameless objects
  30. Return value: Event object handle
  31. //***********************************************************************
  32. //**************************************************************************
  33. Function: msgwaitformultipleobjects//wait until return condition is satisfied returns immediately
  34. //
  35. Return condition: ① one or All (the third parameter) object in the specified (signal) event object (the second parameter) sends a signal
  36. (The wait/timeout time specified by either ② (fourth parameter) has been reached
  37. Satisfied) ③ The specified message (the fifth parameter) has reached the input queue of the thread
  38. //
  39. Parameters: Ncount=1 The number of handles in the specified list is 1
  40. Phandles=event the first element in the specified object handle combination is an Event
  41. Fwaitall=false any object to send a signal
  42. Dwmilliseconds=dwmilliseconds the number of milliseconds to wait is the delay time
  43. Dwwakemask=qs_allinput identifies a specific message type as a message queue for any message
  44. return value: Wait_object_0+1 (ncount) with the specified type of message arrives
  45. //*************************************************************************

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.