index > .NET Base Class Library > Determining which thread is yours in the ProcessThreadCollection ...

Determining which thread is yours in the ProcessThreadCollection ...

After starting a thread using the Thread class,

Getting a ProcessThread collection from the current Process:

Process processInfo = Process.GetCurrentProcess();

ProcessThreadCollection threadsInfo = processInfo.Threads;

Is there a way to determine which ProcessThread instance refers to the Thread you just started.

I tried correlating them comparing

Thread.ManagedThreadId and ProcessThread.Id but they appear to represent different things.

Just looking for a point in the right direction.

Thank you,

Randy




Randy
Randy Cox

As far as I can test, it looks to me that the Thread.ManagedThreadId is the offset in the Process.Threads collection.

So to get the thread id of the current executing thread, you could use:

Process.GetCurrentProcess().Threads[Thread.CurrentThread.ManagedThreadId].Id )

Hope this helps, as I said, I did only a quick test :-)

LucVK

From MSDN:

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads.

That being said, you can PInvoke to GetCurrentThreadId from your managed thread and you will find ProcessThread.Id

Lucian Bargaoanu
reply 3

You can use google to search for other answers

 

More Articles

• not quite sure what .NET technology to use for this
• Specifying user account to run a Thread
• Optimizing performance when enqueuing and dequeuing data
• When does a shared class get initalized, and who init's it?
• Keeping a service running
• Maximized MDI Child Not Maximized
• How to use Type.GetType(string)? Doesnt seem to work.
• SUb MAIN in forms application using VB.NET
• Three bugs?
• .Diagnostics.Process Treats File URL as a Second Class URL?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Enum issues...
• Accessing values from Databound Fields i
• Once my Database is ready...How do i mak
• isDate issue
• adding application path in dill import c#
• App.config and saving the configurations
• Manipulating String
• PInvoke Failure (Couldn't think of a bet
• Problem accessing text file within a res
• Two values behave as one
• Single-Instance - Web-Application
• File or assembly name xxx.dll, or one of
• How scalable are Multicast Events?
• How do you register a C# COM dll via code?
• how to get the symbol of ASCII code in V

Hot Articles

• Using VB .NET 2005 to create an Excel 20
• Access exchange server
• How can I load a form by button?
• xml document to navision via socket
• XML, Excel, VB.NET and some guidance ...
• Custom Web Control - Enable literal cont
• We're not supposed to use List<T>
• Getting the Name of the application from
• Framework assemblies always MSIL?
• Detecting implemented locales
• A class library containing both C# and V
• Windows Service Installation Problem
• convert double to string using ".&q
• Obfuscation by default?
• ResXWriter writing .Language property to

Recommend Articles

• How to convert .pst to .msg
• Preventing multiple instances of an appl
• Creating ASP.Net WebUserControl at RunTi
• Render Control to string
• Service don't start: Errorcode 1053
• Forms and Controls cause 'Out of Memory'
• Use WMI to get the memory size of Remot
• Unable to start debugging on the web ser
• How can instances of ClassB catch and re
• Using .NET Assemblies with a C++ .dll
• Saving configuration settings in .NET 2.0
• Using a SQL Database to authenticate users
• Can I install Visual Studio 2005 on a ma
• Open new console window
• Configuration data writable by all users