Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Thread.Name Property

Gets or sets the name of the thread.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public string Name { get; set; }

Property Value

Type: System.String
A string containing the name of the thread, or null if no name was set.

ExceptionCondition
InvalidOperationException

A set operation was requested, but the Name property has already been set.

This property is write-once. Because the default value of a thread's Name property is null, you can determine whether a name has already been explicitly assigned to the thread by comparing it with null.

The string assigned to the Name property can include any Unicode character.

The following example shows how to name a 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.");
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft