Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 4 Feb 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Naveen K Kohli

United States United States
No Biography provided

Comments and Discussions

 
Questionworks in win7 ?? Pinmemberkiquenet AE24-Nov-11 0:50 
GeneralMy vote of 2 PinmemberGlay17-Apr-10 19:37 
GeneralRetrieving running Windows application list (like those in Task Manager) Pinmembercowboe26-Mar-09 9:12 
Questiongetting list of running applications in computer using C# code Pinmemberer.rohitsingla20-Mar-08 1:03 
GeneralRe: getting list of running applications in computer using C# code Pinmembermaznblue2-Apr-08 9:34 
GeneralRe: getting list of running applications in computer using C# code PinmvpJohn Simmons / outlaw programmer20-Apr-08 2:38 
Generalproblem in Multi threading Pinmembernmehta00526-Feb-07 3:09 
GeneralMonitor the New Open Process PinsussAnonymous21-May-05 11:27 
GeneralRe: Monitor the New Open Process PinmemberNaveen K Kohli22-May-05 2:23 
GeneralRe: Monitor the New Open Process PinsussAnonymous22-May-05 13:22 
GeneralThreads created in an ASP.NET app Pinsussmeareal2-Mar-05 4:28 
GeneralLatest version of this code Pinmemberworkerbuzz23-Dec-04 8:50 
QuestionCan not able to execute.??????? PinmemberHimadrish Laha24-Oct-04 23:41 
GeneralQuery Regarding System.Diagnostics PinmemberLittleScholar23-Oct-04 14:54 
GeneralSystem.Diagnostics.Process Pinmembermehrdada23-Aug-04 11:20 
GeneralRe: System.Diagnostics.Process PinmemberSoftomatix24-Aug-04 2:16 
GeneralRe: System.Diagnostics.Process Pinmembermaluskater27-Oct-06 3:02 
GeneralIs this a very old beta version of C# Pinmemberflamierd23-Aug-03 17:08 
GeneralRe: Is this a very old beta version of C# Pinmembergardia7-Sep-11 21:37 
Generalusing NKDiagnosticUtility; Pinmembermaitre cesar27-Apr-03 22:55 
GeneralRe: using NKDiagnosticUtility; PinmemberSoftomatix28-Apr-03 3:41 
GeneralRe: using NKDiagnosticUtility; Pinmembermaitre cesar30-Apr-03 1:26 
GeneralQuestion : unix process es swiching Pinmemberzhrli20-Oct-01 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 5 Feb 2001
Article Copyright 2001 by Naveen K Kohli
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid