How to determine the current thread

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
 
T

Tomas Restrepo \(MVP\)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top