How to get current cpu usage in C# and VB.NET

To get current cpu usage in C# and VB.NET you can use the following snippet.

Sample C#

public static string GetCurrentCpuUsage()
{
	var cpuCounter = new PerformanceCounter
	{
		CategoryName = "Processor",
		CounterName = "% Processor Time",
		InstanceName = "_Total"
	};
	cpuCounter.NextValue();
	System.Threading.Thread.Sleep(1000);
	return (cpuCounter.NextValue() + "%");
}

Sample VB.NET

Public Shared Function GetCurrentCpuUsage() As String
	Dim cpuCounter = New PerformanceCounter() With { _
		Key .CategoryName = "Processor", _
		Key .CounterName = "% Processor Time", _
		Key .InstanceName = "_Total" _
	}
	cpuCounter.NextValue()
	System.Threading.Thread.Sleep(1000)
	Return (cpuCounter.NextValue() + "%")
End Function

How to get the cpu speed in C# and VB.NET

To get the cpu speed in C# and VB.NET you can use the following snippet.

Sample C#

public uint GetCpuSpeed()
{
  var managementObject = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
  var speed = (uint)(managementObject["CurrentClockSpeed"]);
  managementObject.Dispose();
  return speed;
}

Sample VB.NET

Public Function GetCpuSpeed() As UInteger
	Dim managementObject = New ManagementObject("Win32_Processor.DeviceID='CPU0'")
	Dim speed As UInteger = CUInt(managementObject("CurrentClockSpeed"))
	managementObject.Dispose()
	Return speed
End Function