Эх сурвалжийг харах

20251111002 添加系统运行时间监控

向羽 孟 1 долоо хоног өмнө
parent
commit
56740dccb2

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

@@ -88,8 +88,10 @@
     <Compile Include="ImageAlgorithm\ImageAlgorithmTools.cs" />
     <Compile Include="LogTools\TxtLog.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="SystemTools\SystemRunTimeTools.cs" />
     <Compile Include="UserManager.cs" />
     <Compile Include="WindowsTools\OnScreenKeyboardTools.cs" />
+    <Compile Include="WindowsTools\SystemMonitorClass.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\MvvmScaffoldFrame48.MODEL\MvvmScaffoldFrame48.Model.csproj">

+ 43 - 0
MvvmScaffoldFrame48.DLL/SystemTools/SystemRunTimeTools.cs

@@ -0,0 +1,43 @@
+using MvvmScaffoldFrame48.DLL.ConfigTools;
+using MvvmScaffoldFrame48.Model.StorageModel.SystemTools;
+using System;
+using System.Diagnostics;
+using System.IO;
+
+namespace MvvmScaffoldFrame48.DLL.SystemTools
+{
+    public static class SystemRunTimeTools
+    {
+        static Stopwatch SystemRunTime = new Stopwatch();
+        static long InitRunTime;
+        public static void StartSystemRunTime()
+        {
+            string localDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
+            string systemRunTimePath = Path.Combine(localDataPath, "CCDCount\\SystemRunTime.XML");
+            if (File.Exists(systemRunTimePath))
+            {
+                var systemMessage = XMLReadWrite.DeserializeFromXml<SystemMessageConfigModel>(systemRunTimePath);
+                InitRunTime = systemMessage.SystemRunTime;
+                TimeSpan timeSpan = TimeSpan.FromTicks(InitRunTime);
+            }
+            SystemRunTime.Start();
+        }
+
+        public static void StopSystemRunTime()
+        {
+            string localDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
+            string systemruntimefoundPath = Path.Combine(localDataPath, "CCDCount");
+            if (!Directory.Exists(systemruntimefoundPath))
+            {
+                Directory.CreateDirectory(systemruntimefoundPath);
+            }
+            string systemRunTimePath = Path.Combine(systemruntimefoundPath, "SystemRunTime.XML");
+            SystemRunTime.Stop();
+            long ticks = SystemRunTime.ElapsedTicks;
+            XMLReadWrite.SerializeToXml(new SystemMessageConfigModel()
+            {
+                SystemRunTime = ticks + InitRunTime
+            }, systemRunTimePath);
+        }
+    }
+}

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

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

+ 1 - 0
MvvmScaffoldFrame48.MODEL/MvvmScaffoldFrame48.Model.csproj

@@ -69,6 +69,7 @@
     <Compile Include="StorageModel\ImageAlgorithm\BoundingRectangleMdoel.cs" />
     <Compile Include="StorageModel\SystemAlarm\AlarmMessModel.cs" />
     <Compile Include="StorageModel\SystemAlarm\AlarmTypes.cs" />
+    <Compile Include="StorageModel\SystemTools\SystemMessageConfigModel.cs" />
     <Compile Include="UserModel.cs" />
   </ItemGroup>
   <ItemGroup>

+ 13 - 0
MvvmScaffoldFrame48.MODEL/StorageModel/SystemTools/SystemMessageConfigModel.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.Model.StorageModel.SystemTools
+{
+    public class SystemMessageConfigModel
+    {
+        public long SystemRunTime { get; set; }
+    }
+}