Thread.Name Property


Gets or sets the name of the thread.

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

public string Name {
	[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]

Property Value

Type: System.String

A string containing the name of the thread, or null if no name was set.

Exception Condition

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";
            Console.WriteLine("Unable to name a previously " +
                "named thread.");

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top