SystemMonitorClass.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MvvmScaffoldFrame48.DLL.WindowsTools
  8. {
  9. public class SystemMonitorClass
  10. {
  11. private readonly PerformanceCounter _cpuCounter;
  12. private readonly PerformanceCounter _ramCounter;
  13. Stopwatch stopwatch = Stopwatch.StartNew();
  14. private float HisCPUCounter = 0;
  15. public SystemMonitorClass()
  16. {
  17. _cpuCounter = new PerformanceCounter(
  18. "Processor", "% Processor Time", "_Total");
  19. _ramCounter = new PerformanceCounter(
  20. "Memory", "Available MBytes");
  21. }
  22. public float GetCpuUsage()
  23. {
  24. stopwatch.Stop();
  25. if (stopwatch.ElapsedMilliseconds > 1000)
  26. {
  27. stopwatch.Restart();
  28. HisCPUCounter = _cpuCounter.NextValue();
  29. }
  30. else
  31. {
  32. stopwatch.Start();
  33. }
  34. return HisCPUCounter;
  35. }
  36. public float GetAvailableMemory()
  37. {
  38. return _ramCounter.NextValue();
  39. }
  40. }
  41. }