C # delay function non-sleep

Source: Internet
Author: User

1. Example:

 using   system.runtime.interopservices;[ DllImport (  kernel32.dll   " )]  static  extern  uint   GetTickCount ();  static  void  Delay (uint   ms) { uint  start = GetTickCount ();         while  (GetTickCount ()-Start < ms) {    System.Windows.Forms.Application.DoEvents (); }}

2. Instructions for use:

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

3. Application Scenario:

An example of an API:

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

A to B to send a message, B to execute, at this time a need to wait for B after execution, according to the results of B, again send the next message to B

The example above is a typical time-lapse application.

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

Do not talk about the accuracy of sleep delay, sleep is a fatal weakness, is the process of sleep, the process is unable to respond to external operations, into the state of suspended animation, so that a user experience is very poor, moreover, users want to stop halfway, can only wait, or force kill program.

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

You can use the above example.

Reference: Suzilong11 's column C # delay function non-sleep

C # delay function non-sleep

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.