Thread.Name Właściwość

Definicja

Pobiera lub ustawia nazwę wątku.

public:
 property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
public string? Name { get; set; }
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String

Wartość właściwości

String

Ciąg zawierający nazwę wątku lub null jeśli nie ustawiono żadnej nazwy.

Wyjątki

Zażądano operacji zestawu, ale Name właściwość została już ustawiona.

Przykłady

W poniższym przykładzie pokazano, jak nazwać wątek.

using namespace System;
using namespace System::Threading;
int main()
{
   
   // Check whether the thread has previously been named to
   // avoid a possible InvalidOperationException.
   if ( Thread::CurrentThread->Name == nullptr )
   {
      Thread::CurrentThread->Name =  "MainThread";
   }
   else
   {
      Console::WriteLine( "Unable to name a previously "
      "named thread." );
   }
}
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.");
        }
    }
}
Imports System.Threading

Public Class Name

    <MTAThread> _
    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

Uwagi

Ta właściwość jest zapisywana raz. Ponieważ domyślną wartością właściwości wątku Name jest null, można określić, czy nazwa została już jawnie przypisana do wątku, porównując ją z elementem null.

Ciąg przypisany do Name właściwości może zawierać dowolny znak Unicode.

Dotyczy