Csharp/CSharp Tutorial/Thread/Thread Abort

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Destroying Threads

using System;
using System.Threading;
class MainClass
{
  static int MyCount = 0;
  static void Main(string[] args)
  {
    MyClassThread me = new MyClassThread();
    Thread MyNewThread = new Thread(new ThreadStart(me.MyThread));
    MyNewThread.Start();
    if (MyCount == 0)
      MyNewThread.Abort();
  }
}
class MyClassThread
{
  public void MyThread()
  {
  }
}

Stopping a thread

using System; 
using System.Threading; 
 
class MyThread {  
  public Thread thrd;  
    
  public MyThread(string name) {  
    thrd = new Thread(this.run); 
    thrd.Name = name; 
    thrd.Start();  
  }  
  
  void run() {  
    Console.WriteLine(thrd.Name + " starting."); 
 
    for(int i = 1; i <= 100; i++) {  
      Console.Write(i + " ");  
      Thread.Sleep(50); 
    } 
    Console.WriteLine(thrd.Name + " exiting.");  
  }  
} 
  
class MainClass {  
  public static void Main() {  
    MyThread mt1 = new MyThread("My Thread");  
 
    Thread.Sleep(1000); 
  
    Console.WriteLine("Stopping thread.");  
    mt1.thrd.Abort(); 
 
    mt1.thrd.Join(); 
 
    Console.WriteLine("Main thread terminating.");  
  }  
}
My Thread starting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Stopping thread.
Main thread terminating.

Use Abort(object)

using System; 
using System.Threading; 
 
class MyThread {  
  public Thread thrd;  
    
  public MyThread(string name) {  
    thrd = new Thread(this.run); 
    thrd.Name = name; 
    thrd.Start();  
  }  
  
  void run() {  
    try { 
      Console.WriteLine(thrd.Name + " starting."); 
 
      for(int i = 1; i <= 100; i++) {  
        Console.Write(i + " ");  
        Thread.Sleep(50); 
      } 
      Console.WriteLine(thrd.Name + " exiting normally.");  
    } catch(ThreadAbortException exc) { 
      Console.WriteLine("Thread aborting, code is " + 
                         exc.ExceptionState); 
    } 
  }  
} 
  
class MainClass {  
  public static void Main() {  
    MyThread mt1 = new MyThread("My Thread");  
 
    Thread.Sleep(1000); 
 
    Console.WriteLine("Stopping thread.");  
    mt1.thrd.Abort(100); 
 
    mt1.thrd.Join(); 
 
    Console.WriteLine("Main thread terminating.");  
  }  
}
My Thread starting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Stopping thread.
Thread aborting, code is 100
Main thread terminating.

Use ResetAbort()

using System; 
using System.Threading; 
 
class MyThread {  
  public Thread thrd;  
    
  public MyThread(string name) {  
    thrd = new Thread(this.run); 
    thrd.Name = name; 
    thrd.Start();  
  }  
  
  void run() {  
    Console.WriteLine(thrd.Name + " starting."); 
 
    for(int i = 1; i <= 100; i++) {  
      try { 
        Console.Write(i + " ");  
        Thread.Sleep(50); 
      } catch(ThreadAbortException exc) { 
        if((int)exc.ExceptionState == 0) { 
          Console.WriteLine("Abort Cancelled! Code is " + exc.ExceptionState); 
          Thread.ResetAbort(); 
        } else  
          Console.WriteLine("Thread aborting, code is " + exc.ExceptionState); 
      } 
    } 
    Console.WriteLine(thrd.Name + " exiting normally.");  
  } 
} 
  
class MainClass {  
  public static void Main() {  
    MyThread mt1 = new MyThread("My Thread");  
 
    Thread.Sleep(1000); 
 
    Console.WriteLine("Stopping thread.");  
    mt1.thrd.Abort(100); 
 
    mt1.thrd.Join(); 
 
    Console.WriteLine("Main thread terminating.");  
  }  
}