I'm trying to track the memory and CPU usage using PerformanceCounter , this code works perfectly on some Pcs but on some others it stops in this line
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
and i don't know if it will respond or not because it takes so much time , anyone has an explication for this please ?
here is my code :
public string[] DiagnosticSystem()
{
string[] res = { string.Empty, string.Empty };
try
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
double cp = cpuCounter.NextValue();
while (cp == 0)
{
Thread.Sleep(500);
cp = cpuCounter.NextValue();
res[0] = "Utilisation de CPU : " + Math.Round(cp, 2).ToString(CultureInfo.InvariantCulture) + "%";
}
ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
res[1] = "Mémoire Disponible : " + Math.Round(ramCounter.NextValue() / 1024, 2) + "Mb";
return res;
}
catch (Exception ex)
{
res[0] = ex.ToString();
res[1] = ex.ToString();
return res;
}
}