| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvvmScaffoldFrame48.DLL.WindowsTools
- {
- public class SystemMonitorClass
- {
- private readonly PerformanceCounter _cpuCounter;
- private readonly PerformanceCounter _ramCounter;
- Stopwatch stopwatch = Stopwatch.StartNew();
- private float HisCPUCounter = 0;
- public SystemMonitorClass()
- {
- _cpuCounter = new PerformanceCounter(
- "Processor", "% Processor Time", "_Total");
- _ramCounter = new PerformanceCounter(
- "Memory", "Available MBytes");
- }
- /// <summary>
- /// 获取CPU使用率
- /// </summary>
- /// <returns></returns>
- public float GetCpuUsage()
- {
- stopwatch.Stop();
- if (stopwatch.ElapsedMilliseconds > 1000)
- {
- stopwatch.Restart();
- HisCPUCounter = _cpuCounter.NextValue();
- }
- else
- {
- stopwatch.Start();
- }
- return HisCPUCounter;
- }
- /// <summary>
- /// 获取可用内存
- /// </summary>
- /// <returns></returns>
- public float GetAvailableMemory()
- {
- return _ramCounter.NextValue();
- }
- }
- }
|