C # delay Small function is very useful

Source: Internet
Author: User

Usually when we do WinForm development, sometimes we need to let the program sleep for a few seconds, but if we use the Thread.Sleep () function directly, the page UI will stop responding. How to solve, you can put the page to show the UI of the code into a single-threaded processing, you can also use my following approach, plus a small function OK.
The test is very useful.

 public  static  bool  Delay (int   Delaytime) {Da            Tetime now  = DateTime.Now;             int   S;  do   {TimeSpan Spand                 /span>= datetime.now- now; s  = Spand.                 Seconds;            Application.doevents ();             while  (S < Delaytime);  return  true  ; }

You can also hibernate inside the thread and then go to the main thread.

voidSleep () {Thread thr=NewThread (() =           {               //You can also deal with more time-consuming things here. Thread.Sleep ( +);//Sleep Time                This. Dispatcher.invoke (NewAction (() ={dosomething ();//Invoking processing Events               }));       }); }       //What you have to do after sleep       voiddosomething () {}

You can also use the await Task.delay (20);
Https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.delay%28v=vs.110%29.aspx

C # delay Small function is very useful

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.