A thread's sleep is implemented through the sleep method of the thread class, and the IsAlive property of the instance of the thread class can determine if the thread is finished and the Sleep method is in the following format:
Thread instance. Sleep ();
The sleep time is measured in milliseconds.
Instance
Creates a method that outputs the current thread state without looping 5 times, then executes this method through the thread, and sleeps for 1 milliseconds during execution
Until this method completes
classProgram { Public Static voidmth () {stringState ; for(inti =1; I < +; i++) { //gets the current thread stateState =Thread.CurrentThread.ThreadState.ToString (); if(i%5==0) {Console.WriteLine ("Current thread State"+State ); } } } Static voidMain (string[] args) { //ThreadStart ts = new ThreadStart (MTH);Thread th =NewThread (MTH); Th. Start (); //thread execution state, returns True if the thread sleeps 1 milliseconds for true while(Th. IsAlive) {Console.WriteLine ("thread starts execution"); Thread.Sleep (1); } console.read (); }
Thread Operation thread Hibernation