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