Browse Source

优化读取APP的CPU使用获取方法,调整字节单位转换方法名称

yuzhengyang 7 years ago
parent
commit
c02d77324d

BIN
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide


+ 38 - 15
Fork.Net/Azylee.Utils/Azylee.Core/AppUtils/AppInfoTool.cs

@@ -54,52 +54,75 @@ namespace Azylee.Core.AppUtils
         /// <param name="end"></param>
         /// <param name="interval"></param>
         /// <returns></returns>
+        [Obsolete]
         public static double CalcCpuRate(Process process, TimeSpan begin, int interval)
         {
-            //当前时间
-            var current = process.TotalProcessorTime;
-            //间隔时间内的CPU运行时间除以逻辑CPU数量
-            var minus = current - begin;
-            double value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
+            double value = 0;
+            try
+            {
+                //当前时间
+                var current = process.TotalProcessorTime;
+                //间隔时间内的CPU运行时间除以逻辑CPU数量
+                var minus = current - begin;
+                value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
+            }
+            catch { }
+
             if (value < 0) return 0;
             if (100 < value) return 100;
             return value;
         }
         /// <summary>
-        /// 读取APP占用内存(单位:KB)
+        /// 计算CPU占用率(自动刷新TimeSpan
         /// </summary>
+        /// <param name="process"></param>
+        /// <param name="begin"></param>
+        /// <param name="end"></param>
+        /// <param name="interval"></param>
         /// <returns></returns>
-        public static long RAM()
+        public static double CalcCpuRate(Process process, ref TimeSpan begin, int interval)
         {
-            long value = 0;
-            Process p = null;
+            double value = 0;
             try
             {
-                p = Process.GetCurrentProcess();
-                value = p.WorkingSet64 / 1024;
+                //当前时间
+                var current = process.TotalProcessorTime;
+                //间隔时间内的CPU运行时间除以逻辑CPU数量
+                var minus = current - begin;
+                value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
+
+                begin = process.TotalProcessorTime;
             }
             catch { }
-            finally { p?.Dispose(); }
+
+            if (value < 0) return 0;
+            if (100 < value) return 100;
             return value;
         }
-        public static long RAM(int id)
+        /// <summary>
+        /// 读取APP占用内存(单位:KB)
+        /// </summary>
+        /// <returns></returns>
+        public static long RAM()
         {
             long value = 0;
             Process p = null;
             try
             {
-                p = Process.GetProcessById(id);
+                p = Process.GetCurrentProcess();
                 value = p.WorkingSet64 / 1024;
             }
             catch { }
             finally { p?.Dispose(); }
             return value;
         }
-        public static long RAM(Process p)
+        public static long RAM(int id)
         {
             long value = 0;
+            Process p = null;
             try
             {
+                p = Process.GetProcessById(id);
                 value = p.WorkingSet64 / 1024;
             }
             catch { }

+ 1 - 1
Fork.Net/Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -61,7 +61,7 @@
     <Compile Include="DataUtils\GuidUtils\GuidTool.cs" />
     <Compile Include="DataUtils\SerializeUtils\SerializeTool.cs" />
     <Compile Include="DataUtils\StringUtils\StringTool.cs" />
-    <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
+    <Compile Include="DataUtils\UnitConvertUtils\ByteConvertTool.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressDelegate.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressEventArgs.cs" />
     <Compile Include="IOUtils\BinaryUtils\BinaryFileTool.cs" />

Fork.Net/Azylee.Utils/Azylee.Core/DataUtils/UnitConvertUtils/ByteConvertUtils.cs → Fork.Net/Azylee.Utils/Azylee.Core/DataUtils/UnitConvertUtils/ByteConvertTool.cs