AppInfoTool.cs 4.0 KB

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