Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get running thread by name from ProcessThreadCollection

After searching on Stack Overflow questions and some googling, I still not getting it.

I'm aware that you can check if a single thread is running with "Thread.isAlive()" method, but I want to check if a particular "FooThread" is still running between all running threads from current process, and if not, call the method that starts it.



//somewhere in the code, on another Project/DLL inside solution
private void FooThreadCaller()
{
    Action act = () => fooFunction();
    Thread t = new Thread(new ThreadStart(act));
    t.Name = "FooThread";
    t.Start();
}
//...
Process proc = System.Diagnostics.Process.GetCurrentProcess();
ProcessThreadCollection threads = proc.Threads;
bool ThreadExists = false
foreach (ProcessThread item in threads)
{
    // if item.Name == "FooThread", then ThreadExists = true...
    // so, if !ThreadExists then call FooThreadCaller() and so on.
}
//...

Since the ProcessThread class doesn't have a "Name" property (like System.Threading.Thread does) but only ID, and I only know the thread name ("FooThread") not the ID, how could I check if "FooThread" is running/alive?

Thank you in advance

like image 873
cezarlamann Avatar asked Jun 06 '14 15:06

cezarlamann


People also ask

How do I view all threads running in a process?

You can also toggle on or off thread view mode while top is running, by pressing <H> key. To restrict the top output to a particular process <pid> and check all threads running inside the process: A more user-friendly way to view threads per process is via htop, an ncurses -based interactive process viewer.

How to get a list of processthreads in a process?

using System.Diagnostics; You can get a list of ProcessThreads in a process by using the Process.Threads property, which returns a ProcessThreadCollection object. Here’s a code example for you, assuming that you already have a Process object named theProcess.

How does a process become multithreaded in Linux?

All processes start out single-threaded. This starting thread is usually called the main thread. The main thread may then start new threads in order for the process to become multithreaded, similar to the way a process can call fork () to start a new process.

How to show threads per process using ps command?

You can also use " ps " command to show threads per process. With " ps " we can list LWP (Light Weight process) which depicts Thread ID of the respective process and NWLP (Number of Threads). To show threads per process using ps command you can use below argument 3. Using pstree command You can also use pstree to show threads per process.


3 Answers

As mentioned in the other answers, the ProcessThread and the Thread objects are different. Your dll is creating it's own Thread object, but then it throws away it's reference to that object. Then later on your are querying the OS (through System.Diagnostics) for that thread and expecting the same level of access you had before as the thread's creator.

Instead, you should save the references to your Thread objects as you create them. Maybe your dll could implement a public static collection object which you would then add your thread objects to as you create them.

Then outside of your dll you could loop through the collection and check the names and status of each Thread.

like image 190
Slider345 Avatar answered Sep 27 '22 16:09

Slider345


ProcessThread represents an operating system-level thread, while Thread represents a .Net managed thread. An operating system-level thread doesn't have a name but a unique ID (just like a process does).

Instead of checking in the list of current thread if your thread is still running, you probably should change the logic of your code. For example, you could keep a boolean containing the state of your thread and update it when it starts/ends.

like image 24
ken2k Avatar answered Sep 27 '22 15:09

ken2k


I don't think there is a way to do that. A ProcessThread represents an OS thread. A Thread represents a managed thread inside your application. There isn't really a relationship between the two. Furthermore, the mapping of Thread and ProcessThread is NOT 1:1, as in, a ProcessThread can represent multiple Threads.

like image 27
Vlad Avatar answered Sep 27 '22 16:09

Vlad