Windows based precise timing in VC

Source: Internet
Author: User
Tags current time sleep sleep function time interval

In the industrial production control system, there are many operations that need to be done regularly, such as displaying the current time regularly, periodically refreshing the progress bar on the screen, and sending commands and transmitting data to the upper computer. Especially in the real-time control system and data acquisition system requiring high control performance, precise timing operation is more needed.

As we all know, Windows is a messaging-based system, and any event execution is done by sending and receiving messages. This brings some problems, such as when the CPU of a computer is occupied by a process, or when system resources are tight, messages sent to message queues are temporarily suspended and cannot be processed in real time. Therefore, you cannot simply raise an event that is strictly timed by Windows messages. In addition, because the access to the underlying hardware of the computer is already encapsulated in Windows, it is also difficult to achieve precise timing by directly using the access hardware. Therefore, in practical application, should be in accordance with the specific timing accuracy requirements, to take the appropriate timing method.

VC provides a lot of functions on time operations, using them to control the precise timing and timing operations. This article describes in detail the VC in the Windows based on the precise timing of the seven ways, as shown in the following figure:

Figure One image description

Mode one: VC in the WM_TIMER message mapping can be a simple time control. Call the function SetTimer () first to set the timer interval, such as SetTimer (0,200,null), which is the time interval for setting 200ms. It then adds the timed response function OnTimer () in the application and adds the processing statement of the response to the function to complete the operation at timed time. This timing method is very simple, can realize a certain timing function, but its timing function like sleep () function of the delay function, the precision is very low, the minimum timing precision is only 30ms,cpu occupy low, and the timer message in multitasking operating system priority is very low, can not get timely response, Often can not meet the real-time control of the application of the environment. Only can be used to achieve such as the dynamic display of bitmaps, such as the timing accuracy requirements are not high. such as the Timer1 in the example project.

Mode two: VC use Sleep () function to achieve delay, its unit is MS, such as 2 seconds delay, with Sleep (2000). The precision is very low, the minimum timing precision is only 30ms, the disadvantage of the sleep function is that the delay period can not process other messages, if the time is too long, as if the crash, CPU occupancy rate is very high, can only be used for the delay of the program is not high. such as the Timer2 in the example project.

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.