Display Process list and Processor Performance in C#

Everyone knows, there will be lot of processes running in a system. We can see process list in Task Manager. But it gives limited information. If we want to integrate that Process list in your application, there is no solution for that. But, we can create our own Task Manager to display processes list and processor status.

Now, I am going to create an application that will display processes list and processor performance. I have created this application in VS 2003 in C#.

First, create a web application in C# and name it as ProcessMgr.

Next, add reference to System.Management. I have shown previously, how to add System.Management to our application. It can be added by going to Solution Explorer, Select Add Reference on right-clicking References. In .NET, select System.Management.

Goto WebForm1.aspx code window. Next, add following lines to your code:

using System.IO;

using System.Management;

Next, create UI as shown in below figure:

processlist1.gif 

Here, one grid, one LinkButton, one label is there. Next go to events of Grid in properties window, double click on PageIndexChanged event and write this code:

DataGrid1.CurrentPageIndex =e.NewPageIndex ;

Next go to properties of Grid, set AllowPaging to true. Next format grid in your own way by clicking Auto-Format as shown in figure:

processlist2.gif

Next, add a webform to your solution and name it as WebForm2.aspx. In click event of Next, write this code :

Response.Redirect("WebForm2.aspx");

Write this method call in Page_Load :

processlist();

Next, write this code in code window:

private void processlist()

{

          StreamWriter writer;

          ManagementClass class1=new ManagementClass("Win32_process");

          writer= new StreamWriter("c:\\process.xml",false);

          writer.Write("<?xml version=\"1.0\"?>");

          writer.Write("<processes>");

          foreach(ManagementObject ob in class1.GetInstances())

          {

                   string Caption=ob.GetPropertyValue("Caption").ToString();

                   string Description=ob.GetPropertyValue("VirtualSize").ToString();

                   string Name=ob.GetPropertyValue("WorkingSetSize").ToString();

                   writer.Write("<process>");

                   writer.Write("<Caption>" + Caption +"</Caption>");

                   writer.Write("<VirtualSize>" +Description +"</VirtualSize>");

                   writer.Write("<UserModeTime>" +ob.GetPropertyValue("UserModeTime").ToString() +"</UserModeTime>");

                   writer.Write("<WorkingSetSize>" +Name +"</WorkingSetSize>");

                   writer.Write("<WriteOperationCount>" +ob.GetPropertyValue("WriteOperationCount").ToString() +"</WriteOperationCount>");

                   writer.Write("<WriteTransferCount>" +ob.GetPropertyValue("WriteTransferCount").ToString() +"</WriteTransferCount>");

                   writer.Write("<PageFaults>" +ob.GetPropertyValue("PageFaults").ToString() +"</PageFaults>");

                   writer.Write("<PageFileUsage>" +ob.GetPropertyValue("PageFileUsage").ToString() +"</PageFileUsage>");

                   writer.Write("<Priority>" +ob.GetPropertyValue("Priority").ToString() +"</Priority>");

                   writer.Write("</process>");

          }

          writer.Close();

          File.Copy("c:\\process.xml","c:\\temp1.xml",true);

          StreamWriter writer1=File.AppendText("c:\\temp1.xml");

          writer1.Write("</processes>");

          writer1.Close();

          DataSet ds=new DataSet();

          ds.ReadXml("c:\\temp1.xml");

          DataGrid2.DataSource=ds;

          DataGrid2.DataBind();

          File.Delete("c:\\service.xml");

          File.Delete("c:\\user.xml");

}

Here, I am just creating a xml file with information to be displayed in Grid. By means of ManagementClass and ManagementObject, I am getting status of each process.

Next go to WebForm2.aspx, and add

using System.IO;
using System.Management; to your code.

Next drag a Table control from ToolBox. In properties window of Table, click on Rows Collection and add 9 rows. Each row should contain 2 cells(colums). Here, you can see figure for assistance:

processlist3.gif

processlist4.gif

processlist5.gif

processlist6.gif

processlist7.gif

Create 9 TableRows with 2 cells in each row. Final window,should be like this:

processlist8.gif

Paste this in Page_Load event:

loadprocessordetails();

Next, paste this code in your code window:

#region Processor Details

private void loadprocessordetails()

{

          ManagementClass class1=new ManagementClass("Win32_Processor");

          Table1.Rows[0].Cells[0].Text ="Caption";

          Table1.Rows[1].Cells[0].Text ="Availability";

          Table1.Rows[2].Cells[0].Text ="CPU Status";

          Table1.Rows[5].Cells[0].Text ="LoadPercentage";

          Table1.Rows[3].Cells[0].Text ="ProcessorId";

          Table1.Rows[4].Cells[0].Text ="L2CacheSpeed";

          foreach(ManagementObject ob in class1.GetInstances())

          {

                    Table1.Rows[0].Cells[1].Text =ob.GetPropertyValue("Caption").ToString();

                    Table1.Rows[1].Cells[1].Text =ob.GetPropertyValue("Availability").ToString();

                    Table1.Rows[2].Cells[1].Text =ob.GetPropertyValue("cpustatus").ToString();

                    Table1.Rows[3].Cells[1].Text =ob.GetPropertyValue("ProcessorId").ToString();

                    Table1.Rows[5].Cells[1].Text =ob.GetPropertyValue("LoadPercentage").ToString();

                    if(Convert.ToInt32(ob.GetPropertyValue("LoadPercentage"))<=20)

                    {

                             Table1.Rows[5].Cells[1].BackColor=Color.Green;

                    }

                    else if(Convert.ToInt32(ob.GetPropertyValue("LoadPercentage"))<=50)

                   {

                             Table1.Rows[5].Cells[1].BackColor=Color.Orange ;

                   }

                   else if(Convert.ToInt32(ob.GetPropertyValue("LoadPercentage"))<=80)

                   {

                             Table1.Rows[5].Cells[1].BackColor=Color.OrangeRed ;

                   }

                   else if(Convert.ToInt32(ob.GetPropertyValue("LoadPercentage"))<=95)

                   {

                             Table1.Rows[5].Cells[1].BackColor=Color.Red ;

                   }

                   Table1.Rows[4].Cells[1].Text =ob.GetPropertyValue("L2CacheSpeed").ToString();

          }

          ManagementClass class2=new ManagementClass("win32_operatingsystem");

          foreach(ManagementObject ob1 in class2.GetInstances())

          {

                   Table1.Rows[6].Cells[0].Text="Number of Processes";

                   Table1.Rows[6].Cells[1].Text=ob1.GetPropertyValue("NumberOfProcesses").ToString();

                   Table1.Rows[7].Cells[0].Text="Number of Users";

                   Table1.Rows[7].Cells[1].Text=ob1.GetPropertyValue("NumberOfUsers").ToString();

          }

}

#endregion

This will show details of Processor Performance. Next copy this line to HTML View and paste below Title tag to refresh page for every 5 seconds automatically. So that it will show Processor Performance exactly.

Final window will be like this :

processlist9.gif

processlist10.gif

I hope this code will be useful for all.


Similar Articles