次の方法で共有


Thread.Name プロパティ

スレッドの名前を取得または設定します。

Public Property Name As String
[C#]
public string Name {get; set;}
[C++]
public: __property String* get_Name();public: __property void set_Name(String*);
[JScript]
public function get Name() : String;public function set Name(String);

プロパティ値

スレッドの名前を含む文字列。名前が設定されていない場合は null 参照 (Visual Basic では Nothing) 。

例外

例外の種類 条件
InvalidOperationException 設定操作が要求されましたが、 Name プロパティは既に設定されています。

解説

このプロパティは、一度だけ書き込むことができます。

使用例

[Visual Basic, C#, C++] スレッドに名前を付ける方法の例を次に示します。

 
Imports System
Imports System.Threading

Public Class Name

    Shared Sub Main()

        ' Check whether the thread has previously been named
        ' to avoid a possible InvalidOperationException.
        If Thread.CurrentThread.Name = Nothing Then
            Thread.CurrentThread.Name = "MainThread"
        Else
            Console.WriteLine("Unable to name a previously " & _
                "named thread.")
        End If

    End Sub
End Class

[C#] 
using System;
using System.Threading;

class Name
{
    static void Main()
    {
        // Check whether the thread has previously been named
        // to avoid a possible InvalidOperationException.
        if(Thread.CurrentThread.Name == null)
        {
            Thread.CurrentThread.Name = "MainThread";
        }
        else
        {
            Console.WriteLine("Unable to name a previously " +
                "named thread.");
        }
    }
}

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

void main()
{
    // Check whether the thread has previously been named to
    // avoid a possible InvalidOperationException.
    if(Thread::CurrentThread->Name == 0)
    {
        Thread::CurrentThread->Name = "MainThread";
    }
    else
    {
        Console::WriteLine(S"Unable to name a previously "
            S"named thread.");
    }
}

[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

参照

Thread クラス | Thread メンバ | System.Threading 名前空間