0

I'm not sure if such is possible in a managed code. Let me explain. I need to know that a particular thread is currently running within my process by its ID. Can I do that from another thread in the same process?

c00000fd
  • 20,994
  • 29
  • 177
  • 400

4 Answers4

2

I suppose by Id you mean Managed Id. Short answer is there is no straight way to enumerate managed threads in a process. From a different process you can use managed debugging API (MDbg) to attach to the target process and enumerate all the managed threads from a debugger perspective. If you can launch a separate process to do it, this is probably the easiest way. If you have to do it from the same process, it is also possible. But you will need to look into MDbg source code. The newest MDbg supports attaching to a dump file. You need to implement your own ICorDebugDataTarget to support a live process. Then you can access your process from the same process the same way you attaching to a dump file using MDbg. There are some limitation just like analyzing a dump file is not the same as analyzing a live process. But it will definitely satisfy your need - enumerate the managed thread Id in the process.

But as I said, if it's possible for you to spawn a short lived process to do the job, that is easiest.

You can download MDbg source code and sample from here.

treehouse
  • 2,521
  • 1
  • 21
  • 39
  • I see. I was able to accomplish this by using the Win32 (unmanaged) thread IDs and thread creation time. Thanks. – c00000fd May 20 '13 at 04:11
1

You can't map a ProccessThread id to a managed Thread id, as explained here.

You can't enumerate all managed threads in your process, as explained here.

If you need this functionality, maintain your own collection of managed thread IDs.

Community
  • 1
  • 1
mbeckish
  • 10,485
  • 5
  • 30
  • 55
  • This is easier if OP is just interested in threads that he/she explicitly creates - not threads in ThreadPool or created by 3rd libraries. – treehouse May 20 '13 at 01:40
  • @KaiWang - I guess it depends on what the OP means by "thread is currently running". If he means "the task has not yet completed", then that can be tracked, regardless of whether it is implemented via Thread, ThreadPool, I/O Control Ports, etc. If he means "is in the middle of its time slice from the scheduler and is currently executing on another CPU/core", then no, that can't be done. – mbeckish May 20 '13 at 12:48
0

I don't think you can do this with managed threads. If you really need this functionality you may be able to accomplish what you want by using explicitly created threads and maintaining them by creating your own thread manager. Each time you launch a new thread you can assign it an ID and store it in a dictionary. That way it should be easy to look up a thread by its ID to get the info you need from it. When a thread completes execution you can then remove it from the dictionary if you don't need it anymore.

public class ThreadManager
{
    private int threadID { get; set; }
    public Dictionary<int, Thread> ThreadList { get; set; }

    public ThreadManager()
    {
        this.ThreadList = new Dictionary<int, Thread>();
    }

    public void LaunchThread(Action<string> SomeProcess, string s)
    {
        Thread thread = new Thread(() => SomeProcess(s));
        int id = threadID++;
        thread.Name = id.ToString();
        ThreadList.Add(id, thread);
        thread.Start();
    }

    public void KillThread()
    {
        ThreadList.Remove(Int32.Parse(Thread.CurrentThread.Name));
    }
}
David DeMar
  • 2,390
  • 2
  • 32
  • 45
-3

I found this in a forum:

int threadId = [something];

Process currentProcess = Process.GetCurrentProcess();

foreach( Thread thread in currentProcess.Threads )
{
    if( thread.ManagedThreadId.Equals( threadId ) )
    {
        // do something
    }
}

Haven't tried it myself.

Scott Isaacs
  • 1,168
  • 7
  • 16
  • Thanks, but that won't work. I get the following exception on the `foreach` line: `System.InvalidCastException: Unable to cast object of type 'System.Diagnostics.ProcessThread' to type 'System.Threading.Thread'.` – c00000fd May 20 '13 at 00:58
  • ProcessThread != Thread. ProcessThread.Id is the native thread id. – treehouse May 20 '13 at 01:32