Publish Article
Go Freelance

.Net Thread Monitoring Application


If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.



This is the second article in a series of showing how System.Diagnostics classes in .NET SDK can be used to get information about the running process. In my first article “Using Diagnostic Classes In The .NET SDK”, I tried to create an application that shows some static information about the processes that are running on a machine. In this article, I will try to explain the usage of one more Diagnostic class, System.Diagnostic.ProcessThread. As the name of the class suggests, it is all the information about the threads.

The basic concepts about process creation and running remain the same in .NET Framework. When a process is created, it starts in a main thread. And inside this main thread, it can create as many worker or UI threads as it wants, to optimize and speed up the process. The Diagnostic class Process provides information about the threads that are running inside a process. You can call the Threads property on System.Diagnostics.Process class to get an array of System.Diagnostics.ProcessThrerad objects. The following code shows that the GetProcessByID method from the Process class can be used to get the Process object and then we can get the threads associated with that process.

public static ProcessThread [] GetProcessThreads (int nProcID)
      Process proc = Process.GetProcessById (nProcID);
      ProcessThread [] threads = proc.Threads;
      return threads;
   catch ( Exception e)
      Console.WriteLine (e.Message);
      return null;

The ProcessThread class exposes a bunch of properties that can be used to get complete information about a thread. Now that we have the thread objects, we can use them to get their properties like their ID and priority.

What Is The ID Of The Thread?

Every thread has a unique ID assigned to it while it is alive. You can call the Id property of the thread to get this unique ID.

m_nThreadIDs[i] = m_Threads[i].Id;

What Is The Priority Of The Thread?

Every thread has a base priority. Depending on the time slice allocation by the Operating System and other factors, this priority gets changed throughout the life of a thread. This priority can go from as low as Idle state to as high as Real Time state. You can call BasePriority and CurrentPriority properties to get these values.


What Is The Current Thread State?

Every thread that gets created in a process changes it states throughout its life. It can be in any of the following states at any given time: Initialized, Running, Waiting, etc. The ThreadState property of the ProcessThread class gives the current state information about a given thread. This property returns an enumerator, ThreadState. I have provided a static function, ThreadStateToString, in the accompanying utility class that translates this enumerator into a string.

m_ThreadStates[i] = m_Threads[i].ThreadState;

Why Is A Thread In The Waiting State?

There is always some reason when a thread is put in a waiting state. You can make use of the WaitReason property to get it. This property returns an enumerator, ThreadWaitReason. There is a static function, ThreadWaitReasonToString, in my utility class that translates this enumerator into a string. Here is a word of caution. If the thread is not in Wait state, do not call ThreadWaitReason property. The documentation does not tell you, but it is going to throw an exception if you make this call on a non-waiting thread. I learned it the hard way while I was writing this application.

if (ThreadState.Wait == m_ThreadStates[i])
   m_ThreadWaitReasons[i] = m_Threads[i].WaitReason;

How Much Time Has The Thread Spent Running?

The ProcessThread class provides three properties that will give you the time a thread has spent in the operating system core, application portion and total time. These properties are PrevilegedProcessorTime, UserProcessorTime and TotalProcessorTime, respectively.

m_TotalProcessorTimes[i] = m_Threads[i].TotalProcessorTime;

Thread Monitoring Application

The code accompanying this article is a diagnostic application that monitors all the threads running inside a process. I have set up a timer in the application that updates the thread states every two seconds. For your convenience, you can modify this value in the MainAppForm constructor.

m_Timer.Interval = 2000;

This application displays a list of all currently running processes on the system. When you click on one of the processes in this list, the list view on the right hand side starts showing the states of all the threads running inside that process. The code has not been optimized to take care of the flickers that occur due to refreshing of the list view every two seconds, but it works. Try this application and see how the thread states show up for any of your processes.

Netomatix team appreciates everybody's continued support and contribution to site. Join The Effort


There are no comments for this article. Be the first to post a comment.

Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix