Working With Process Class

Introduction

This article is aimed at introducing the novice .NET developer to the basic usage of System.Diagnostics services included in the .NET BCL. More specifically, it will show how to enumerate the current processes as well as retrieving process relative information such as modules loaded, thread usage, handle usage and much more. Included with this article is a sample .NET console application (developed in C#) that works as a primitive process list viewer to help developers get started. Feel free to modify this sample to suit your own needs.

In order to get at the process information via the BCL, you will need to make use of the Process class contained in the System.Diagnostics namespace. The Process class contains several useful properties/methods that allow developers to access process relative information. Below you will find a simple example of how to use the Process class to retrieve the name of the current process:

using System ;
using System.Diagnostics ;
public class Test
{
public static void Main ( String[] args )
{
Process CurrentProcess = Process.GetCurrentProcess ( ) ;
Console.WriteLine ( "Image Name: {0}", CurrentProcess.ProcessName ) ;
}
}

It is important to note that the Process class retrieves various types of information for a process running on the system and that this information is cached. This means that if your application requires up-to-date information about process information, you should frequently refresh the cache by calling the Process.Refresh method. In addition to simply retrieving the name of the current process, more information can be obtained. Some examples include:

  • Memory Statistics
  • Process Times
  • Thread Statistics
  • Handle Statistics

If you need to make the jump to unmanaged code and you need the native process handle, you can use the Handle property of the Process class. Keep in mind that the handle is only valid if the component started the process.

When you plan on enumerating all processes currently running on the system there is a possibility that you do not have access to process specific information. If the Process class ever fails (usually with ACCESS_DENIED) it will throw the corresponding exception. Example of an application trying to get process information and failing:

System.ComponentModel.Win32Exception: Access is denied

This only scratches the surface of the System.Diagnostics namespace by examining the Process class. There are numerous other useful classes in this namespace that allows you to collect performance data (via performance counters made easy), debug information etc.

PList usage

Syntax:

PList <ImageName>

If you do not specify an image name, PList will list all accessible processes running on the system with some process information. If you specify an image name, PList will display information about that specific process. If the ImageName is a dll, PList will display which processes currently has the the dll loaded as well as the base address.

If you enter an image name without the extension, PLIst will default to the executable extension.

To build PList

csc /t:exe plist.cs


Similar Articles