How to determine the current thread

  • Thread starter Thread starter Howard Weiss
  • Start date Start date
H

Howard Weiss

I would like to know what thread my form is running on in a Managed C++
Windows Form Application

I tried

using namespace System::Diagnostics;
using namespace System::Threading;

and including the code

Debug::WriteLine(Thread::CurrentThread->Name->ToString());

in my form constructor

This compiles but results in a Null Reference exception

Howard Weiss
 
Howard,
I would like to know what thread my form is running on in a Managed C++
Windows Form Application

I tried

using namespace System::Diagnostics;
using namespace System::Threading;

and including the code

Debug::WriteLine(Thread::CurrentThread->Name->ToString());

in my form constructor

This compiles but results in a Null Reference exception

First of all, there's no need to call ToString(), the Name property is
already a string.

Second of all, it's perfectly normal for a thread to have no name (indeed it
probably won't unless you set it yourself). Now, in Win32, one would usually
identify the thread by it's thread id. However, in managed code, you don't
normally would get that since theoretically, managed threads don't need to
map one-to-one to the underlying OS threads (you can still call the Win32
GetCurrentThreadId() api). If all you want to do is distinguish one managed
thread from another, you can use it's hashcode.
 
Back
Top