Selaa lähdekoodia

20251107001 添加系统资源监控类

向羽 孟 2 viikkoa sitten
vanhempi
säilyke
bcd025b1e8

+ 1 - 0
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -112,6 +112,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UserManager.cs" />
     <Compile Include="WindowsTools\OnScreenKeyboardTools.cs" />
+    <Compile Include="WindowsTools\SystemMonitorClass.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\MvvmScaffoldFrame48.MODEL\MvvmScaffoldFrame48.Model.csproj">

+ 46 - 0
MvvmScaffoldFrame48.DLL/WindowsTools/SystemMonitorClass.cs

@@ -0,0 +1,46 @@
+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");
+        }
+
+        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();
+        }
+    }
+}