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
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.
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.
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.
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.
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.
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.
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 Thread
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With