Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Thread.Name Property

Gets or sets the name of the thread.

[Visual Basic]
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);

Property Value

A string containing the name of the thread, or a null reference (Nothing in Visual Basic) if no name was set.

Exceptions

Exception Type Condition
InvalidOperationException A set operation was requested, and the Name property has already been set.

Remarks

This property is write-once.

Example

[Visual Basic, C#, C++] The following code example shows how to name a thread.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Thread Class | Thread Members | System.Threading Namespace

Show:
© 2017 Microsoft