Click here to Skip to main content
15,355,659 members
Articles / Programming Languages / C#
Posted 16 Jan 2001


62 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.10/5 (7 votes)
4 Feb 2001
Using the ProcessThread class to monitor the states of threads inside a process.

Sample Image - ThreadMonitor.jpg


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.


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


About the Author

Naveen K Kohli
United States United States
No Biography provided

Comments and Discussions

Questionworks in win7 ?? Pin
kiquenet.com24-Nov-11 0:50
professionalkiquenet.com24-Nov-11 0:50 
GeneralMy vote of 2 Pin
Glay17-Apr-10 19:37
MemberGlay17-Apr-10 19:37 
GeneralRetrieving running Windows application list (like those in Task Manager) Pin
cowboe26-Mar-09 9:12
Membercowboe26-Mar-09 9:12 
Questiongetting list of running applications in computer using C# code Pin
er.rohitsingla20-Mar-08 1:03
Memberer.rohitsingla20-Mar-08 1:03 
GeneralRe: getting list of running applications in computer using C# code Pin
maznblue2-Apr-08 9:34
Membermaznblue2-Apr-08 9:34 
GeneralRe: getting list of running applications in computer using C# code Pin
#realJSOP20-Apr-08 2:38
mva#realJSOP20-Apr-08 2:38 
Generalproblem in Multi threading Pin
NVMehta26-Feb-07 3:09
MemberNVMehta26-Feb-07 3:09 
GeneralMonitor the New Open Process Pin
Anonymous21-May-05 11:27
MemberAnonymous21-May-05 11:27 
GeneralRe: Monitor the New Open Process Pin
Naveen K Kohli22-May-05 2:23
MemberNaveen K Kohli22-May-05 2:23 
GeneralRe: Monitor the New Open Process Pin
Anonymous22-May-05 13:22
MemberAnonymous22-May-05 13:22 
GeneralThreads created in an ASP.NET app Pin
mea2-Mar-05 4:28
Membermea2-Mar-05 4:28 
GeneralLatest version of this code Pin
workerbuzz23-Dec-04 8:50
Memberworkerbuzz23-Dec-04 8:50 
QuestionCan not able to execute.??????? Pin
Populate12324-Oct-04 23:41
MemberPopulate12324-Oct-04 23:41 
GeneralQuery Regarding System.Diagnostics Pin
LittleScholar23-Oct-04 14:54
MemberLittleScholar23-Oct-04 14:54 
GeneralSystem.Diagnostics.Process Pin
Member 73394323-Aug-04 11:20
MemberMember 73394323-Aug-04 11:20 
GeneralRe: System.Diagnostics.Process Pin
Softomatix24-Aug-04 2:16
MemberSoftomatix24-Aug-04 2:16 
GeneralRe: System.Diagnostics.Process Pin
maluskater27-Oct-06 3:02
Membermaluskater27-Oct-06 3:02 
GeneralIs this a very old beta version of C# Pin
flamierd23-Aug-03 17:08
Memberflamierd23-Aug-03 17:08 
GeneralRe: Is this a very old beta version of C# Pin
gardia7-Sep-11 21:37
Membergardia7-Sep-11 21:37 
Generalusing NKDiagnosticUtility; Pin
maitre cesar27-Apr-03 22:55
Membermaitre cesar27-Apr-03 22:55 
GeneralRe: using NKDiagnosticUtility; Pin
Softomatix28-Apr-03 3:41
MemberSoftomatix28-Apr-03 3:41 
GeneralRe: using NKDiagnosticUtility; Pin
maitre cesar30-Apr-03 1:26
Membermaitre cesar30-Apr-03 1:26 
GeneralQuestion : unix process es swiching Pin
zhrli20-Oct-01 18:56
Memberzhrli20-Oct-01 18:56 

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

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