AppInfoTool.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.ProcessUtils;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. namespace Azylee.Core.AppUtils
  14. {
  15. public class AppInfoTool
  16. {
  17. /// <summary>
  18. /// 读取APP Processor(可读取App的CPU使用率)
  19. /// </summary>
  20. /// <returns></returns>
  21. [Obsolete]
  22. public static PerformanceCounter Processor()
  23. {
  24. Process p = null;
  25. PerformanceCounter processor = null;
  26. try
  27. {
  28. p = Process.GetCurrentProcess();
  29. processor = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
  30. }
  31. catch { }
  32. return processor;
  33. }
  34. /// <summary>
  35. /// 读取进程CPU使用率(同名进程无法支持)
  36. /// </summary>
  37. /// <param name="p"></param>
  38. /// <returns></returns>
  39. [Obsolete]
  40. public static PerformanceCounter Processor(Process p)
  41. {
  42. PerformanceCounter processor = null;
  43. try
  44. {
  45. string name = ProcessTool.GetInstanceNameById(p.Id);
  46. if (!string.IsNullOrWhiteSpace(name))
  47. {
  48. processor = new PerformanceCounter("Process", "% Processor Time", name);
  49. }
  50. }
  51. catch { }
  52. return processor;
  53. }
  54. /// <summary>
  55. /// 计算CPU占用率
  56. /// </summary>
  57. /// <param name="process"></param>
  58. /// <param name="begin"></param>
  59. /// <param name="end"></param>
  60. /// <param name="interval"></param>
  61. /// <returns></returns>
  62. [Obsolete]
  63. public static double CalcCpuRate(Process process, TimeSpan begin, int interval)
  64. {
  65. double value = 0;
  66. try
  67. {
  68. //当前时间
  69. var current = process.TotalProcessorTime;
  70. //间隔时间内的CPU运行时间除以逻辑CPU数量
  71. var minus = current - begin;
  72. value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
  73. }
  74. catch { }
  75. if (value < 0) return 0;
  76. if (100 < value) return 100;
  77. return value;
  78. }
  79. /// <summary>
  80. /// 计算CPU占用率(自动刷新TimeSpan)
  81. /// </summary>
  82. /// <param name="process"></param>
  83. /// <param name="begin"></param>
  84. /// <param name="end"></param>
  85. /// <param name="interval"></param>
  86. /// <returns></returns>
  87. public static double CalcCpuRate(Process process, ref TimeSpan begin, int interval)
  88. {
  89. double value = 0;
  90. try
  91. {
  92. //当前时间
  93. var current = process.TotalProcessorTime;
  94. //间隔时间内的CPU运行时间除以逻辑CPU数量
  95. var minus = current - begin;
  96. value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
  97. begin = process.TotalProcessorTime;
  98. }
  99. catch { }
  100. if (value < 0) return 0;
  101. if (100 < value) return 100;
  102. return value;
  103. }
  104. /// <summary>
  105. /// 读取APP占用内存(单位:KB)
  106. /// </summary>
  107. /// <returns></returns>
  108. public static long RAM()
  109. {
  110. long value = 0;
  111. Process p = null;
  112. try
  113. {
  114. p = Process.GetCurrentProcess();
  115. value = p.WorkingSet64 / 1024;
  116. //value = p.PeakWorkingSet64 / 1024;
  117. }
  118. catch { }
  119. finally { p?.Dispose(); }
  120. return value;
  121. }
  122. public static long RAM(int id)
  123. {
  124. long value = 0;
  125. Process p = null;
  126. try
  127. {
  128. p = Process.GetProcessById(id);
  129. value = p.WorkingSet64 / 1024;
  130. }
  131. catch { }
  132. finally { p?.Dispose(); }
  133. return value;
  134. }
  135. }
  136. }