Thread.Abort 方法


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

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

備注:

1、如果對尚未啟動的線程調用 Abort,則當調用 Start 時該線程將中止。如果對被阻止或正在休眠的線程調用 Abort,則該線程被中斷然后中止。

2、如果在已掛起的線程上調用 Abort,則將在調用 Abort 的線程中引發 ThreadStateException,並將 AbortRequested 添加到被中止的線程的 ThreadState 屬性中。 直到調用 Resume 后,才在掛起的線程中引發 ThreadAbortException

3、如果在正在執行非托管代碼的托管線程上調用 Abort,則直到線程返回到托管代碼才引發 ThreadAbortException。

4、如果同時出現兩次對 Abort 的調用,則可能一個調用設置狀態信息,而另一個調用執行 Abort。但是,應用程序無法檢測到此情況。

5、對線程調用了 Abort 后,線程狀態包括 AbortRequested。 成功調用 Abort 而使線程終止后,線程狀態更改為 Stopped。如果有足夠的權限,作為 Abort 目標的線程就可以使用 ResetAbort 方法取消中止操作。有關說明如何調用 ResetAbort 方法的示例,請參見 ThreadAbortException 類。

 

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

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();

   //如果注釋掉 Thread.ResetAbort();  fianlly中的代碼仍會執行,但try catch finally之后的代碼不會被執行.而后跳轉到調用abort()的函數中(這里為主函數)

  }
        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  獲取額外的中止信息。
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM