How to get the name of a thread?

How do I retrieve the name of the current thread?
C++ win console app using MS VS 2010 .net

in my my main line I delcalre thread1 and set the name property. If I look in the watch window the name is there. See below:
// set name and start up thread
thread1->Name = L"Thread1";
// watch window shows thread1->Name is "Thread1"
thread1->Start();


The thread runs but when I look at the name of the current thread it is <undefined value>. See the code below:
// inside of thread1 process, get the current thread name
Thread ^current = Thread::CurrentThread;
// watch window shows current->Name is <undefined value>
Console::WriteLine(current->Name);

How do I retrieve the name of the current thread?

Curtis
I'll admit that I am NOT a .Net fan, but according to MSDN the Thread class has a member function for the string property named "set()", I'd imagine that is what you want to use to set the name. http://msdn.microsoft.com/en-us/library/system.threading.thread.name.aspx
I believe -/IDOC-
-/IDOC- ?
Hi there, by default Thread.Name is set to a null reference. You can change this by setting it to a value, but only once! If you attempt to set a thread's name more than once, you'll get a InvalidOperationException, so it's best to check to see whether it has a name or not before fiddling with it.

1
2
3
4
5
6
7
8
9
10
// Check to see if the current thread has a name
if(Thread::CurrentThread->Name == 0)
{
  // You can safely set the name
  Thread::CurrentThread->Name = "HomerSimpson";
}
else
{
  Console::WriteLine(S"DOH!");
}
Last edited on
Topic archived. No new replies allowed.