ARTICLE

Display Process list and Processor Performance in VB.NET

Posted by Sateesh Arveti Articles | VB.NET Tutorials December 19, 2006
This article shows how to display Processes list and performance of processor exactly.
 
Reader Level:


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 VB.NET.

First, create a web application in VB.NET 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:

Imports System.IO

Imports System.Management

 

Next, create UI as shown in below figure:

 

 

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:

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 Sub processlist()

    Dim writer As StreamWriter

    Dim class1 As ManagementClass = New ManagementClass("Win32_process")

    writer = New StreamWriter("c:\process.xml", False)

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

    writer.Write("<processes>")

    For Each ob As ManagementObject In class1.GetInstances

        Dim Caption As String = ob.GetPropertyValue("Caption").ToString

        Dim Description As String = ob.GetPropertyValue("VirtualSize").ToString

        Dim Name As String = 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>")

    Next

    writer.Close()

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

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

    writer1.Write("</processes>")

    writer1.Close()

    Dim ds As DataSet = New DataSet

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

    DataGrid2.DataSource = ds

    DataGrid2.DataBind()

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

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

End Sub

 

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

Imports System.IO

Imports System.Management 

 

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:

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

Paste this in Page_Load event:

loadprocessordetails()

Next, paste this code in your code window:

#Region "Processor Detail" 

Private Sub loadprocessordetails()

    Dim class1 As ManagementClass = 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"

    For Each ob As ManagementObject 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 Then

            Table1.Rows(5).Cells(1).BackColor = Color.Green

        Else

            If Convert.ToInt32(ob.GetPropertyValue("LoadPercentage")) <= 50 Then

                Table1.Rows(5).Cells(1).BackColor = Color.Orange

            Else

                If Convert.ToInt32(ob.GetPropertyValue("LoadPercentage")) <= 80 Then

                    Table1.Rows(5).Cells(1).BackColor = Color.OrangeRed

                Else

                    If Convert.ToInt32(ob.GetPropertyValue("LoadPercentage")) <= 95 Then

                        Table1.Rows(5).Cells(1).BackColor = Color.Red

                    End If

                End If

            End If

        End If

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

    Next

    Dim class2 As ManagementClass = New ManagementClass("win32_operatingsystem")

    For Each ob1 As ManagementObject 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

    Next

End Sub

#End Region

 

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 :

I hope this code will be useful for all.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor