Can I get the Process from the ManagedThreadId?

D

DC

Hi,

I want to log cpu consumption of individual threads and I also want to
expose the id's of the native threads the managed threads are using
(this number is usefull to check performance in process explorer).

All I am able to retrieve easily is the ManagedThreadId. I could use
what I have found in another forum (see code below) to get the process
info of the current thread. But I have a collection of all
ManagedThreadId's of all plugins my application is using already
available and would like to go from there.

So what I need is something like:

GetNativeThreadIdFromManagedThreadId(Int32 managedThreadId)

TIA for any ideas.

Regards
DC


[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId",
ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();

// Call this passing in 0
public static ProcessThread GetProcessThreadFromWin32ThreadId(Int32
threadId)
{
if (threadId == 0) threadId = ThreadUtility.GetCurrentWin32ThreadId
();
foreach (Process process in Process.GetProcesses())
{
foreach (ProcessThread processThread in process.Threads)
{
if (processThread.Id == threadId) return processThread;
}
}
throw new InvalidOperationException("No thread matching specified
thread Id was found.");
}
 
U

usenet

Hi,

I want to log cpu consumption of individual threads and I also want to
expose the id's of the native threads the managed threads are using
(this number is usefull to check performance in process explorer).

All I am able to retrieve easily is the ManagedThreadId. I could use
what I have found in another forum (see code below) to get the process
info of the current thread. But I have a collection of all
ManagedThreadId's of all plugins my application is using already
available and would like to go from there.

So what I need is something like:

GetNativeThreadIdFromManagedThreadId(Int32 managedThreadId)

TIA for any ideas.

Regards
DC

                [DllImport("Kernel32", EntryPoint = "GetCurrentThreadId",
ExactSpelling = true)]
                public static extern Int32 GetCurrentWin32ThreadId();

                // Call this passing in 0
                public static ProcessThread GetProcessThreadFromWin32ThreadId(Int32
threadId)
                {
                        if (threadId == 0) threadId = ThreadUtility.GetCurrentWin32ThreadId
();
                        foreach (Process process in Process.GetProcesses())
                        {
                                foreach (ProcessThread processThread in process.Threads)
                                {
                                        if (processThread.Id == threadId) return processThread;
                                }
                        }
                        throw new InvalidOperationException("No thread matching specified
thread Id was found.");
                }

It is conceptually incorrect to assume a managed thread maps to a
single win32 native-thread.

As an implementational reality, a managed thread can be serviced by
many fibres (light-weight threads).

If you are interested in native threads then deal get a list of native
threads from the process id irrespective of managed threads.

I suspect that's why MS have ommitted NativeThreadID's from the
interface.

Regards,

Sphere 10 Software (www.sphere10.com)
File Server Migrator - Industrial Strength File Copier
http://www.sphere10.com/fileservermigrator/
 
D

DC

I want to log cpu consumption of individual threads and I also want to
expose the id's of the native threads the managed threads are using
(this number is usefull to check performance in process explorer).
All I am able to retrieve easily is the ManagedThreadId. I could use
what I have found in another forum (see code below) to get the process
info of the current thread. But I have a collection of all
ManagedThreadId's of all plugins my application is using already
available and would like to go from there.
So what I need is something like:
GetNativeThreadIdFromManagedThreadId(Int32 managedThreadId)
TIA for any ideas.
Regards
DC

                [DllImport("Kernel32", EntryPoint = "GetCurrentThreadId",
ExactSpelling = true)]
                public static extern Int32 GetCurrentWin32ThreadId();
                // Call this passing in 0
                public static ProcessThread GetProcessThreadFromWin32ThreadId(Int32
threadId)
                {
                        if (threadId == 0) threadId = ThreadUtility.GetCurrentWin32ThreadId
();
                        foreach (Process process in Process.GetProcesses())
                        {
                                foreach(ProcessThread processThread in process.Threads)
                                {
                                       if (processThread.Id == threadId) return processThread;
                                }
                        }
                        throw new InvalidOperationException("No thread matching specified
thread Id was found.");
                }

It is conceptually incorrect to assume a managed thread maps to a
single win32 native-thread.

As an implementational reality, a managed thread can be serviced by
many fibres (light-weight threads).

If you are interested in native threads then deal get a list of native
threads from the process id irrespective of managed threads.

I suspect that's why MS have ommitted NativeThreadID's from the
interface.

Regards,

Sphere 10 Software (www.sphere10.com)
File Server Migrator - Industrial Strength File Copierhttp://www.sphere10..com/fileservermigrator/

Thank you. Is there any way to find out the "TotalProcessorTime" of
individual managed threads?

Regards
DC
 
U

usenet

Hi,
I want to log cpu consumption of individual threads and I also want to
expose the id's of the native threads the managed threads are using
(this number is usefull to check performance in process explorer).
All I am able to retrieve easily is the ManagedThreadId. I could use
what I have found in another forum (see code below) to get the process
info of the current thread. But I have a collection of all
ManagedThreadId's of all plugins my application is using already
available and would like to go from there.
So what I need is something like:
GetNativeThreadIdFromManagedThreadId(Int32 managedThreadId)
TIA for any ideas.
Regards
DC
                [DllImport("Kernel32", EntryPoint ="GetCurrentThreadId",
ExactSpelling = true)]
                public static extern Int32 GetCurrentWin32ThreadId();
                // Call this passing in 0
                public static ProcessThread GetProcessThreadFromWin32ThreadId(Int32
threadId)
                {
                        if (threadId == 0) threadId = ThreadUtility.GetCurrentWin32ThreadId
();
                        foreach (Process process in Process.GetProcesses())
                        {
                                foreach (ProcessThread processThread in process.Threads)
                                {
                                        if (processThread.Id == threadId) return processThread;
                                }
                        }
                        throw new InvalidOperationException("No thread matching specified
thread Id was found.");
                }
It is conceptually incorrect to assume a managed thread maps to a
single win32 native-thread.
As an implementational reality, a managed thread can be serviced by
many fibres (light-weight threads).
If you are interested in native threads then deal get a list of native
threads from the process id irrespective of managed threads.
I suspect that's why MS have ommitted NativeThreadID's from the
interface.

Sphere 10 Software (www.sphere10.com)
File Server Migrator - Industrial Strength File Copierhttp://www.sphere10.com/fileservermigrator/

Thank you. Is there any way to find out the "TotalProcessorTime" of
individual managed threads?

Regards
DC- Hide quoted text -

- Show quoted text -

Doesn't look like there is.

Regards,

Sphere 10 Software (www.sphere10.com)
File Server Migrator - Copy Local Users and Groups
http://www.sphere10.com/fileservermigrator/
 

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