次の方法で共有


ThreadAbortException.ExceptionState プロパティ

スレッドの中止に関連するアプリケーション固有の情報を含むオブジェクトを取得します。

Public ReadOnly Property ExceptionState As Object
[C#]
public object ExceptionState {get;}
[C++]
public: __property Object* get_ExceptionState();
[JScript]
public function get ExceptionState() : Object;

プロパティ値

アプリケーション固有の情報を含むオブジェクト。

解説

このプロパティから返されるオブジェクトは、 Abort メソッドの stateInfo パラメータで指定します。このオブジェクトの正確な内容と使用法はアプリケーションで定義されます。通常、このオブジェクトは中止されるスレッドに重要な情報を伝達するために使用されます。

使用例

[Visual Basic, C#, C++] 中止するスレッドに情報を渡す方法の例を次に示します。

 
Imports System
Imports System.Threading

Public Class Test

    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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class Test
{
    Test() {}
public:
    static void TestMethod()
    {
        try
        {
            while(true)
            {
                Console::WriteLine(S"New thread running.");
                Thread::Sleep(1000);
            }
        }
        catch(ThreadAbortException* abortException)
        {
            Console::WriteLine(dynamic_cast<String*>(
                abortException->ExceptionState));
        }
    }
};

    void main()
    {
        Thread* newThread = 
            new Thread(new ThreadStart(0, &Test::TestMethod));
        newThread->Start();
        Thread::Sleep(1000);

        // Abort newThread.
        Console::WriteLine(S"Main aborting new thread.");
        newThread->Abort(S"Information from main.");

        // Wait for the thread to terminate.
        newThread->Join();
        Console::WriteLine(S"New thread terminated - main exiting.");
    }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

ThreadAbortException クラス | ThreadAbortException メンバ | System.Threading 名前空間 | CurrentThread | Thread.Abort | スレッドの破棄