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

Please rate this snippet

How to kill a process in Batch

To kill a process in Batch you can use the snippets below.
If you dont know how to retrieve the PID you can use our other snippet List all Processes in Batch.

Sample Batches

taskkill /PID 1292

if you want to force the taskkill you can use /F.

taskkill /F /PID 1292

if you want to taskkill the process by name you can use the follwoing snippet.

taskkill /im notepad++.exe

for more informations see the msdn TASKKILL

Please rate this snippet