12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- public class SystemMonitor
- {
- private readonly PerformanceCounter _cpuCounter;
- private readonly PerformanceCounter _ramCounter;
- Stopwatch stopwatch = Stopwatch.StartNew();
- private float HisCPUCounter = 0;
- public SystemMonitor()
- {
- _cpuCounter = new PerformanceCounter(
- "Processor", "% Processor Time", "_Total");
- _ramCounter = new PerformanceCounter(
- "Memory", "Available MBytes");
- }
- public float GetCpuUsage()
- {
- stopwatch.Stop();
- if (stopwatch.ElapsedMilliseconds > 1000)
- {
- stopwatch.Restart();
- HisCPUCounter = _cpuCounter.NextValue();
- }
- else
- {
- stopwatch.Start();
- }
- return HisCPUCounter;
- }
- public float GetAvailableMemory()
- {
- return _ramCounter.NextValue();
- }
- }
|