Thread.Abort 方法

[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)] 
 public void Abort()  
在調用此方法的線程上引起 ThreadAbortException,以開始終止此線程的過程。 調用此方法一般會終止線程。

在線程上調用此方法時,系統在線程中引起 ThreadAbortException 以停止它。 ThreadAbortException 是一個能夠由應用程序代碼捕獲的特殊異常,但除非調用 ResetAbort,不然會在 catch 塊的結尾再次引起它。ResetAbort 取消停止請求,並防止 ThreadAbortException 終止該線程。未執行的 finally 塊將在線程終止前執行。函數

備註:線程

一、若是對還沒有啓動的線程調用 Abort,則當調用 Start 時該線程將停止。若是對被阻止或正在休眠的線程調用 Abort,則該線程被中斷而後停止。orm

二、若是在已掛起的線程上調用 Abort,則將在調用 Abort 的線程中引起 ThreadStateException,並將 AbortRequested 添加到被停止的線程的 ThreadState 屬性中。 直到調用 Resume 後,纔在掛起的線程中引起 ThreadAbortExceptionget

三、若是在正在執行非託管代碼的託管線程上調用 Abort,則直到線程返回到託管代碼才引起 ThreadAbortException。string

四、若是同時出現兩次對 Abort 的調用,則可能一個調用設置狀態信息,而另外一個調用執行 Abort。可是,應用程序沒法檢測到此狀況。it

五、對線程調用了 Abort 後,線程狀態包括 AbortRequested。 成功調用 Abort 而使線程終止後,線程狀態更改成 Stopped。若是有足夠的權限,做爲 Abort 目標的線程就可使用 ResetAbort 方法取消停止操做。有關說明如何調用 ResetAbort 方法的示例,請參見 ThreadAbortException 類。io

 

using System;
using System.Threading;
using System.Security.Permissions;form

public class ThreadWork {
    public static void DoWork()
    {
        try
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Thread - working.");
                Thread.Sleep(100);
            }
        }
        catch (ThreadAbortException e)
        {
            Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
            Console.WriteLine("Thread.state:{0}", Thread.CurrentThread.ThreadState.ToString());
            Console.WriteLine("Exception message: {0}", e.Message);
            Thread.ResetAbort();class

   //若是註釋掉 Thread.ResetAbort();  fianlly中的代碼仍會執行,但try catch finally以後的代碼不會被執行.然後跳轉到調用abort()的函數中(這裏爲主函數)thread

  }
        finally
        {
         Console.WriteLine("Thread in finally.Thread.state:{0}",Thread.CurrentThread.ThreadState.ToString());

        }

 //若是運行  Thread.ResetAbort();  如下代碼仍會執行,然後跳轉到調用abort()的函數中(這裏爲主函數)
        Console.WriteLine("Thread - still alive and working.");

Console.WriteLine("Thread.state:{0}", Thread.CurrentThread.ThreadState.ToString());
        Thread.Sleep(1000);
        Console.WriteLine("Thread - finished working.");
    }
}

class ThreadAbortTest
{
    public static void Main()
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        Thread myThread = new Thread(myThreadDelegate);
        myThread.Start();
        Thread.Sleep(100);
        Console.WriteLine("Main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("Thread.state:{0}", myThread.ThreadState.ToString());
        Console.Read();

}
}

 

public void Abort(Object stateInfo)

在調用此方法的線程上引起 ThreadAbortException,以開始終止此線程。

並提供有關線程終止的異常信息的過程。調用此方法一般會終止線程。

using System;
using System.Threading;

class Test
{
    public static void Main()
    {
        Thread newThread = new Thread(new ThreadStart(TestMethod));
        newThread.Start();
        Thread.Sleep(1000);

        // Abort newThread.
        Console.WriteLine("Main aborting new thread.");
        newThread.Abort("Information from Main.");

   //停止線程,並提供object類型的有關停止的信息。

        // Wait for the thread to terminate.
        newThread.Join();
        Console.WriteLine("New thread terminated - Main exiting.");
        Console.Read();
    }

    static void TestMethod()
    {
        try
        {
            while (true)
            {
                Console.WriteLine("New thread running.");
                Thread.Sleep(1000);
            }
        }
        catch (ThreadAbortException abortException)
        {
            Console.WriteLine((string)abortException.ExceptionState);

   //abortException.ExceptionState  獲取額外的停止信息。        }    }}

相關文章
相關標籤/搜索