Thread::Name Property

 

Gets or sets the name of the thread.

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

public:
property String^ Name {
	String^ get();
	[HostProtectionAttribute(SecurityAction::LinkDemand, ExternalThreading = true)]
	void set(String^ value);
}

Property Value

Type: System::String^

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

Exception Condition
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 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." );
   }
}

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