Hi Waghmare,
Refer below example to get CPU and Memory utilisation.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label ID="lblCPU" runat="server" /><br />
<asp:Label ID="lblRam" runat="server" />
<asp:Timer runat="server" OnTick="OnTick" Interval="1000"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Diagnostics;
VB.Net
Imports System.Diagnostics
Code
C#
protected static PerformanceCounter CPUCounter;
protected static PerformanceCounter memoryCounter;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
CPUCounter = new PerformanceCounter();
CPUCounter.CategoryName = "Processor";
CPUCounter.CounterName = "% Processor Time";
CPUCounter.InstanceName = "_Total";
memoryCounter = new PerformanceCounter();
memoryCounter.CategoryName = "Memory";
memoryCounter.CounterName = "Available MBytes";
}
}
protected void OnTick(object sender, EventArgs e)
{
// Check the if condition for value greater than 90.
lblCPU.Text = GetCpuUsage().ToString("N2") + "%";
lblRam.Text = GetMemory() + "MB";
}
public float GetCpuUsage()
{
return CPUCounter.NextValue();
}
public float GetMemory()
{
return memoryCounter.NextValue();
}
VB.Net
Protected Shared CPUCounter As PerformanceCounter
Protected Shared memoryCounter As PerformanceCounter
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
CPUCounter = New PerformanceCounter()
CPUCounter.CategoryName = "Processor"
CPUCounter.CounterName = "% Processor Time"
CPUCounter.InstanceName = "_Total"
memoryCounter = New PerformanceCounter()
memoryCounter.CategoryName = "Memory"
memoryCounter.CounterName = "Available MBytes"
End If
End Sub
Protected Sub OnTick(ByVal sender As Object, ByVal e As EventArgs)
' Check the if condition for value greater than 90.
lblCPU.Text = GetCpuUsage().ToString("N2") & "%"
lblRam.Text = GetMemory() & "MB"
End Sub
Public Function GetCpuUsage() As Single
Return CPUCounter.NextValue()
End Function
Public Function GetMemory() As Single
Return memoryCounter.NextValue()
End Function
Output
46.94%
306MB