Interrupt method, you can only break a thread that is in the WaitSleepJoin state, and the thread resumes execution when the thread is not waitsleepjoin.
Calling the interrupt method produces a threadinterruptexception exception
Class Program {static void Main (string[] args) {Stayawake stayawake = new Stayawake (
);
Thread newthread = new Thread (new ThreadStart (Stayawake.threadmethod));
Newthread.start ();
Newthread.name = "New Thread";
Newthread.interrupt ();
Console.WriteLine ("Main thread calls interrupt on new thread");
Stayawake.sleepswitch = true;
Console.WriteLine ("1stat" + Thread.CurrentThread.ThreadState);
Newthread.join ();
} class Stayawake {bool Sleepswitch = false;
public bool Sleepswitch {set {Sleepswitch = value; }} public void Threadmethod () {Console.WriteLine () ' New thread ' is executing thread me
Thod ");
while (!sleepswitch) {thread.spinwait (10000000);
} try {Console.WriteLine ("2name" + Thread.CurrentThread.Name);
Console.WriteLine ("2stat" + Thread.CurrentThread.ThreadState);
Console.WriteLine ("New thread going to sleep");
Thread.Sleep (Timeout.infinite); catch (ThreadInterruptedException e) {Console.WriteLine ("New thread can not go"
To sleep-"+" interrupted by main thread "); }
}
}