Retrieve Information About the Current Process

Introduction

Consider a scenario where you are debugging an application and you find out that your application begins to run sluggishly after it has been running for a while. Sometimes this happens very rapidly, and at other times it takes four of five hours for the application to slow down. you are able to determine that the application slow down occurs when the total processor time exceeds 10 minutes. You need to write some code that checks the total processor time and perform the necessary cleanup when the total processor time crosses 10 minutes. You can do this by using the GetCurrentProcess method of Process class.

You can use the GetCurrentProcess method of the Process class to retrieve information about the current process. This information my include the duration for which the process has been in execution or the system resources that the process utilize. You can use this information to identify performance issues or to check for the availability of resources required to run another process.

The following code example shows how to Retrieve current process information using GetCurrentProcess method of Process class.

Code

class Currentprocess

{

public void GetcProcess()

{

Process CurrentProcess = Process.GetCurrentProcess();


Console.WriteLine("Current Process ID: "+CurrentProcess.Id);

Console.WriteLine("Current Process Name: " + CurrentProcess.ProcessName);

Console.WriteLine("Total Process Time: " + +CurrentProcess.TotalProcessorTime);

}