Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
HI FRINDS!
I want to know how can i get the ID thread ?(not the console mode,the visual mode) +_+ thank you
Posted
Comments
Sergey Alexandrovich Kryukov 25-Apr-11 11:24am    
Why are your asking about "console mode" or "visual mode".
There are no such "modes", but more importantly, is has nothing to do with threading.
It shows that you're quite confused with threading.
Also, you almost never need the ID, so I would be curious why?
--SA

Thread.CurrentThread.ManagedThreadId should work.
 
Share this answer
 
Comments
Kim Togo 25-Apr-11 10:55am    
My 5. :-)
Sergey Alexandrovich Kryukov 25-Apr-11 11:25am    
Correct, a 5.
--SA
Call to get the current managed thread id.

int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;


If you what the current process id where all your threads lives.

int processId = System.Diagnostics.GetCurrentProcess().Id;
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Apr-11 11:27am    
At the same time with d@anish. Well, my 5, too. Process ID is maybe irrelevant, but I suspect OP does not really need any of those ID.
--SA
dan!sh 25-Apr-11 11:32am    
May be for logging. Just a guess.
Sergey Alexandrovich Kryukov 25-Apr-11 11:41am    
Maybe, but why guessing? OP could have been more collaborative. Sticking to a minimal information or demanding "answer just this" is not very productive.
Thank you.
--SA
dan!sh 25-Apr-11 11:46am    
There many "hit and run" kind of threads around here. I have asked a lot of things out of curiosity and result was OP never came back. May be my curiosity was ignored but nonetheless guessing is the only option IMHO left.
Sergey Alexandrovich Kryukov 25-Apr-11 12:00pm    
I know...
--SA
You've already been answered. But here's some info that may not be obvious. ManagedThreadId returns the unique ID associated with the managed thread. This is not the ID of the underlying native thread. So you cannot pass this to any native API that requires a threadId parameter.

For that you need to P/Invoke GetCurrentThreadId from the running thread. Beware though. It's risky to cache this value because under certain circumstances the CLR may move a managed thread into a different native thread (it's extremely rare, but is not an impossible scenario). So you need to call this prior to using it so you know you can use it safely. If you use it within the same method, the CLR cannot move the managed thread for the duration of that method call.

Another hack people use is to intentionally introduce a native stack frame into the call stack. This will prevent the CLR from moving the managed thread into a new native thread since that will completely wreck the call stack. I can't see any really good reasons to resort to such extreme measures though.
 
Share this answer
 
Comments
Kim Togo 25-Apr-11 12:57pm    
Thanks for the inside :-)
Nish Nishant 25-Apr-11 13:04pm    
You're welcome. Not sure why, but someone game me a 3!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900