ThreadAbortException.ExceptionState 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得一個包含與執行緒中止相關的應用程式特定資訊的物件。
public:
property System::Object ^ ExceptionState { System::Object ^ get(); };
public object ExceptionState { get; }
member this.ExceptionState : obj
Public ReadOnly Property ExceptionState As Object
屬性值
一個包含特定應用程式資訊的物件。
範例
以下程式碼範例說明如何將資訊傳遞給正在中止的執行緒。
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.");
// Wait for the thread to terminate.
newThread.Join();
Console.WriteLine("New thread terminated - Main exiting.");
}
static void TestMethod()
{
try
{
while(true)
{
Console.WriteLine("New thread running.");
Thread.Sleep(1000);
}
}
catch(ThreadAbortException abortException)
{
Console.WriteLine((string)abortException.ExceptionState);
}
}
}
Imports System.Threading
Public Class Test
<MTAThread> _
Shared Sub Main()
Dim newThread As New Thread(AddressOf TestMethod)
newThread.Start()
Thread.Sleep(1000)
' Abort newThread.
Console.WriteLine("Main aborting new thread.")
newThread.Abort("Information from Main.")
' Wait for the thread to terminate.
newThread.Join()
Console.WriteLine("New thread terminated - Main exiting.")
End Sub
Shared Sub TestMethod()
Try
While True
Console.WriteLine("New thread running.")
Thread.Sleep(1000)
End While
Catch abortException As ThreadAbortException
Console.WriteLine( _
CType(abortException.ExceptionState, String))
End Try
End Sub
End Class
備註
此屬性回傳的物件是透過 stateInfo 方法的 Abort 參數來指定的。 此物件的具體內容與用途由應用定義;它通常用來傳達對中止執行緒有意義的資訊。