2

I want to get the performance data like in the performance tab in the task manager window.

I got this code:

using (PerformanceCounter pCounter = new PerformanceCounter())
{               
    pCounter.CategoryName = "Processor"; //this get me the cpu usage
    pCounter.CounterName = "% Processor Time";
    pCounter.InstanceName = "_Total";

    // will always start at 0
    pCounter.NextValue();
    System.Threading.Thread.Sleep(1000);
    //now matches task manager reading
    float cpuUsage = pCounter.NextValue();

    pCounter.CategoryName = "Memory";
    pCounter.CounterName = "Available MBytes"; //this gets me the available memory
    pCounter.InstanceName = string.Empty;
}

I also need:

  • The up time (time the server is active HH:mm:ss)
  • Number of processes
  • Number of threads
  • Ethernet usage

I have no idea how to get this data...

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • I don't know what is it... – Liran Friedman Feb 05 '15 at 13:46
  • [Number of processes](https://msdn.microsoft.com/en-us/library/1f3ys1f9%28v=vs.110%29.aspx). [Number of threads](https://msdn.microsoft.com/en-us/library/system.collections.readonlycollectionbase.count(v=vs.110).aspx) Ethernet usage is slightly more complicated, found a [CodeReview.SE](http://codereview.stackexchange.com/questions/28367/basic-network-utilisation-display) article. [Server up time](http://stackoverflow.com/a/972189/3191303). Sometimes breaking each item down into separate problems will help identify a solution easier than the whole. – AWinkle Feb 05 '15 at 13:50
  • Please add some more tags to your question- windows .net etc. – Reenactor Rob Feb 05 '15 at 13:52
  • AWinkle, thanks for the links. I'll try implementing them. – Liran Friedman Feb 05 '15 at 13:58
  • AWinkle, thanks for the links.The server up time works well but the other 2 are not quite what I need. I need the total process count and thread count. Do you know where I can find a list of all category names for the PerformanceCounter ? maybe I'll get it from there... – Liran Friedman Feb 05 '15 at 14:32

1 Answers1

0

The .net framework provides the System.Diagnostics class with a plethora of methods to access system info including performance data.

https://msdn.microsoft.com/en-us/library/vstudio/System.Diagnostics%28v=vs.90%29.aspx

Reenactor Rob
  • 1,508
  • 1
  • 11
  • 20