Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. // just an example
  2. string categoryName = "Processor";
  3. string counterName = "% Processor Time";
  4. string host = "<name or IP>"; // PRTG's %host
  5. string instanceName = "_Total";
  6.  
  7. if (PerformanceCounterCategory.Exists(categoryName, host) &&
  8.     PerformanceCounterCategory.CounterExists(counterName, categoryName, host) &&
  9.     PerformanceCounterCategory.InstanceExists(instanceName, categoryName, host))
  10. {
  11.     // despite the above verifications, sometimes a "category doesn't exist", "counter doesn't exist" or "instance doesn't exist" exception still occurs!
  12.    
  13.     PerformanceCounter counter = string.IsNullOrEmpty(instanceName) ?
  14.         new PerformanceCounter(categoryName, counterName, true) { MachineName = host } :
  15.         new PerformanceCounter(categoryName, counterName, instanceName, host) { ReadOnly = true };
  16.    
  17.     counter.BeginInit();
  18.  
  19.     // some counters need the previous value
  20.     counter.NextValue();
  21.     Thread.Sleep(1000);
  22.  
  23.     // the needed value
  24.     float value = counter.NextValue();
  25.  
  26.     counter.Close();
  27.     counter.Dispose();
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement