CPU Usage
-
:confused:Dear All, I want to check the CPU Usage in C#. May be through Perfprmance counters?? Can I get sample code for that. With Regards Manoj
-
:confused:Dear All, I want to check the CPU Usage in C#. May be through Perfprmance counters?? Can I get sample code for that. With Regards Manoj
What type of CPU usage you want to check is important. You can browse the various performance counters and read their descriptions using the Server Explorer tab in Visual Studio - right-clicking on the perf counter you want and select Properties - or use perfmon.exe o NT-based Windows OSes. An example of just getting values every .5 seconds follows:
using System;
using System.Diagnostics;
using System.Threading;
class Usage
{
static void Main()
{
using (PerformanceCounter pc = new PerformanceCounter())
{
// Use a specific counter for all CPUs.
pc.CategoryName = "Processor";
pc.CounterName = "% User Time";
pc.InstanceName = "_Total";
Console.Error.WriteLine("Total % User Time:");
Console.Error.WriteLine("Press any key to exit...");
ThreadPool.QueueUserWorkItem(new WaitCallback(Count), pc);
Console.Read();
Console.Error.WriteLine("Done");
}
}
static void Count(object state)
{
PerformanceCounter pc = state as PerformanceCounter;
while (pc != null)
{
Console.WriteLine("{0,3:f2}%", pc.NextValue());
Thread.Sleep(500);
}
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
-
What type of CPU usage you want to check is important. You can browse the various performance counters and read their descriptions using the Server Explorer tab in Visual Studio - right-clicking on the perf counter you want and select Properties - or use perfmon.exe o NT-based Windows OSes. An example of just getting values every .5 seconds follows:
using System;
using System.Diagnostics;
using System.Threading;
class Usage
{
static void Main()
{
using (PerformanceCounter pc = new PerformanceCounter())
{
// Use a specific counter for all CPUs.
pc.CategoryName = "Processor";
pc.CounterName = "% User Time";
pc.InstanceName = "_Total";
Console.Error.WriteLine("Total % User Time:");
Console.Error.WriteLine("Press any key to exit...");
ThreadPool.QueueUserWorkItem(new WaitCallback(Count), pc);
Console.Read();
Console.Error.WriteLine("Done");
}
}
static void Count(object state)
{
PerformanceCounter pc = state as PerformanceCounter;
while (pc != null)
{
Console.WriteLine("{0,3:f2}%", pc.NextValue());
Thread.Sleep(500);
}
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
:confused:Thanks you lot. Actually I want to implement cluster. in which I want to check CPU Usage before assigning it to computation. With Regards Manoj
-
:confused:Thanks you lot. Actually I want to implement cluster. in which I want to check CPU Usage before assigning it to computation. With Regards Manoj
You can still use the performance counters and should consider taking a brief running average before giving more work to a CPU. CPU usage goes up and down and performance counters only have so much resolution between ticks (no where near as fast as a CPU tick). There is actually APIS for clustering in Windows available either as automation objects (which you can use with a Runtime-Callable Wrapper (RCW) created by tlbimp.exe) or native functions you can P/Invoke. See http://msdn.microsoft.com/library/en-us/mscs/mscs/windows_clustering.asp[^] for details. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]