Jeremy
Jeremy

Reputation: 941

How to determine whether a Thread object is the current thread in .NET?

Suppose I have a reference to a Thread object, such as:

System.Threading.Thread thread;

If I want to determine whether the current thread is represented by this object, should the following code be used?

bool currentThread = (thread.ManagedThreadId == Thread.CurrentThread.ManagedThreadId);

I'm pretty sure this will work fine, but it seems strange that there wouldn't be a more direct method for checking this. Is there one?

Upvotes: 3

Views: 1161

Answers (1)

Platinum Azure
Platinum Azure

Reputation: 46183

Just use Thread.CurrentThread:

bool currentThread = (thread == Thread.CurrentThread);

Upvotes: 9

Related Questions