C # delay function non-sleep__ function

Source: Internet
Author: User

In many cases, our program, before performing the next step, will need to wait for the execution of the previous one, which is most evident in the asynchronous operation, or in the program that uses the message loop mechanism to communicate.

An example of an API:

Our own program is a, need to use API combined with Windows message mechanism, control external program B;

A send a message to B, B to execute, at this time a need to wait for B execution, according to B's execution result, again send the next message to B

The above example, is a very typical delay application.

When it comes to delay, you may be the first to react.

Let's not talk about the accuracy of sleep delay, sleep of a fatal weakness, is the process of sleep, the process is not responsive to external operations, into the state of suspended animation, so, one is the user experience is very poor, moreover, users want to stop halfway, can only wait, or mandatory kill off the program.

Therefore, the sleep at this time is not advisable. What we need is a delay function that can continue to respond to the operation with high precision.

Below, we implement one ourselves:

Using System.Runtime.InteropServices;
[DllImport ("kernel32.dll")]

static extern uint GetTickCount ();
 
static void Delay (UINT ms) {
    UINT start = GetTickCount ();
    while (GetTickCount ()-Start < ms) {
        application.doevents ();
    }
}
Related Article

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.