Christopher B
Christopher B

Reputation: 11

Get Consistent CPU Usage in a Windows Form Application

Does anyone have an idea on how to get a consistent CPU usage for several servers displayed on a windows form app?

I am experiencing issues where a number of servers are consistently running high and I need to keep a close eye. So I have built a Windows form app for a restart, and an RDP option already but I would like to add CPU to the side of each server's label.

So if it's possible how can I get it to then notify me once it reaches a certain thresh-hold?

Thanks. Chris

Upvotes: 1

Views: 1765

Answers (2)

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14545

Use PerformanceCounters to peek processor usage in a local area network. These lines of code will get the processor usage in the current machine (the machine name is the fourth parameter in the constructor):

PerformanceCounter perf = new PerformanceCounter("Processor", "% Processor Time", "_Total", ".");
double procUsage = perf.NextValue(); // first reading returns 0.0 for this kind of PerfCounter
procUsage = perf.NextValue(); // Now the procUsage variable holds the real processor usage

Just be aware that the first read operation will return 0.0, but next readings will return the approrpiate value.

Upvotes: 1

CodingWithSpike
CodingWithSpike

Reputation: 43698

One option is to use a System.Diagnostics.PerformanceCounter to monitor the CPU counter on each of your servers. This would give you basically the same numbers that you would get out of Windows Performance Monitor (PerfMon).

Upvotes: 0

Related Questions