Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET Diagnostics - II, A Threads Monitoring Application Using C#

0.00/5 (No votes)
4 Feb 2001 2  
Using the ProcessThread class to monitor the states of threads inside a process.

Sample Image - ThreadMonitor.jpg

Introduction

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)
{
   try
   {
      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.

m_Threads[i].BasePriority;
m_Threads[i].CurrentPriority;

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here