Latency operations in VC Functions

Source: Internet
Author: User
Tags sleep function

When it comes to latency in the program, how do you think about it and start a new thread? If my program only uses a single thread, but wants the function to wait for 10 seconds to return the value, and cannot process other messages as if I used the sleep function?

Here I will summarize several latency methods that can be seen in the Forum. In addition, I am not responsible for studying other people's sources. If this article is useful to everyone, please thank the authors in this article (the ID on csdn ):Laiyiling (the most familiar stranger),Qunkangli),Tyzyx (Sunday Island).

From the perspective of the stranger's processing method, this is the time span with the largest delay, measured in at least seconds:
Http://community.csdn.net/Expert/FAQ/FAQ_Index.asp? Id = 195559
I have seen more than one person ask. In fact, it is estimated that the stranger directly handwritten this code, not from the program segment copy out, there are some hand mistakes, you just need to adjust it.
# Include

Coledatetime start_time = coledatetime: getcurrenttime ();
Coledatetimespan end_time = coledatetime: getcurrenttime ()-start_time;
While (end_time.gettotalseconds () <= 2)
{
MSG;
Getmessage (& MSG, null, 0, 0 );
Translatemessage (& MSG );
Dispatchmessage (& MSG );
End_time = coledatetime: getcurrenttime ()-start_time;
}
I noticed that
Pretranslatemessage (& MSG );
To:
Translatemessage (& MSG );
Dispatchmessage (& MSG );
The reason is that it can be used not only in MFC, but also with limited pretranslatemessage, and may cause thread message blocking.
Another note is that the member functions of the coledatetimespan class include:
Gettotalminutes, gettotalhours, and gettotaldays can implement latency for a larger period of time.

To a smaller time span, use gettickcount to execute millisecond-level latency:
DWORD dwstart = gettickcount ();
DWORD dwend = dwstart;
Do
{
MSG;
Getmessage (& MSG, null, 0, 0 );
Translatemessage (& MSG );
Dispatchmessage (& MSG );
Dwend = gettickcount ();
} While (dwend-dwstart) <= 2000 );

Then the latency in microseconds:
Large_integer litmp;
Longlong qpart1, qpart2;
Double D = 0;
Queryperformancecounter (& litmp );
// Obtain the Initial Value
Qpart1 = litmp. quadpart;
While (d <40) // The time you want
{
Queryperformancecounter (& litmp );
Qpart2 = litmp. quadpart;
D = (double) (qpart2-qpart1 );
}
Source: http://community.csdn.net/Expert/TopicView1.asp? Id = 2663023. No changes have been made. If you need to process messages in microsecond-level latency, please refer to the queue for modification.

In the end, if it still cannot be met, do the time delay of the clock cycle:

# Define nop_count 3 // you need to calculate by yourself according to the NOP and loop instructions.
_ ASM {
MoV ECx, nop_count
Delay: NOP
Loop delay
}
However, is it a little bit ......

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.