Raises a ThreadStopException in the thread, which results in the thread's death unless the thread catches and ignores the exception.
[Visual Basic] Public Sub Stop() [C#] public void Stop(); [C++] public: void Stop(); [JScript] public function Stop();
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the appropriate SecurityPermission. |
When calling Stop, there is no guarantee that the thread actually does stop; the thread could catch and ignore the exception. If the caller of Stop wants to know if the call really stopped the thread, Join must be called after the call to Stop. Calling Join forces the caller to "wait" until the thread is truly stopped.
[C#]
public class TX { public TX( ) { } public void run( ) { try { while (true) { StringBuilder oSB = new StringBuilder("AB"); oSB.Append("C"); } } finally { Console.WriteLine("Cleanup"); } } public static void Main(String[] args) { TX tx = new TX( ); Thread t1 = new Thread(new ThreadStart(tx.run)); t1.Start(); Thread.Sleep(2000); t1.Stop(); t1.Join(); } }