CpuMonitor.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using System.IO;
  6. using System.Text;
  7. using System.Management;
  8. using System.Runtime.InteropServices;
  9. using DW5S.App;
  10. namespace DW5S.App
  11. {
  12. public class CpuMonitor
  13. {
  14. private int m_ProcessorCount = 0; //CPU个数
  15. private PerformanceCounter pcCpuLoad; //CPU计数器
  16. private long m_PhysicalMemory = 0; //物理内存
  17. private const int GW_HWNDFIRST = 0;
  18. private const int GW_HWNDNEXT = 2;
  19. private const int GWL_STYLE = (-16);
  20. private const int WS_VISIBLE = 268435456;
  21. private const int WS_BORDER = 8388608;
  22. #region AIP声明
  23. [DllImport("IpHlpApi.dll")]
  24. extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
  25. [DllImport("User32")]
  26. private extern static int GetWindow(int hWnd, int wCmd);
  27. [DllImport("User32")]
  28. private extern static int GetWindowLongA(int hWnd, int wIndx);
  29. [DllImport("user32.dll")]
  30. private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
  31. [DllImport("user32", CharSet = CharSet.Auto)]
  32. private extern static int GetWindowTextLength(IntPtr hWnd);
  33. #endregion
  34. #region 构造函数
  35. /// <summary>
  36. /// 构造函数,初始化计数器等
  37. /// </summary>
  38. public CpuMonitor()
  39. {
  40. //初始化CPU计数器
  41. pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  42. pcCpuLoad.MachineName = ".";
  43. pcCpuLoad.NextValue();
  44. //CPU个数
  45. m_ProcessorCount = Environment.ProcessorCount;
  46. //获得物理内存
  47. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  48. ManagementObjectCollection moc = mc.GetInstances();
  49. foreach (ManagementObject mo in moc)
  50. {
  51. if (mo["TotalPhysicalMemory"] != null)
  52. {
  53. m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
  54. }
  55. }
  56. }
  57. #endregion
  58. #region CPU个数
  59. /// <summary>
  60. /// 获取CPU个数
  61. /// </summary>
  62. public int ProcessorCount
  63. {
  64. get
  65. {
  66. return m_ProcessorCount;
  67. }
  68. }
  69. #endregion
  70. #region CPU占用率
  71. /// <summary>
  72. /// 获取CPU占用率
  73. /// </summary>
  74. public float CpuLoad
  75. {
  76. get
  77. {
  78. return pcCpuLoad.NextValue();
  79. }
  80. }
  81. #endregion
  82. #region 可用内存
  83. /// <summary>
  84. /// 获取可用内存
  85. /// </summary>
  86. public long MemoryAvailable
  87. {
  88. get
  89. {
  90. long availablebytes = 0;
  91. //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory");
  92. //foreach (ManagementObject mo in mos.Get())
  93. //{
  94. // availablebytes = long.Parse(mo["Availablebytes"].ToString());
  95. //}
  96. ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
  97. foreach (ManagementObject mo in mos.GetInstances())
  98. {
  99. if (mo["FreePhysicalMemory"] != null)
  100. {
  101. availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
  102. }
  103. }
  104. return availablebytes;
  105. }
  106. }
  107. #endregion
  108. #region 物理内存
  109. /// <summary>
  110. /// 获取物理内存
  111. /// </summary>
  112. public long PhysicalMemory
  113. {
  114. get
  115. {
  116. return m_PhysicalMemory;
  117. }
  118. }
  119. #endregion
  120. #region 结束指定进程
  121. /// <summary>
  122. /// 结束指定进程
  123. /// </summary>
  124. /// <param name="pid">进程的 Process ID</param>
  125. public static void EndProcess(int pid)
  126. {
  127. try
  128. {
  129. Process process = Process.GetProcessById(pid);
  130. process.Kill();
  131. }
  132. catch { }
  133. }
  134. #endregion
  135. public int ProcessThread()
  136. {
  137. return Process.GetCurrentProcess().Threads.Count;
  138. }
  139. public long ProcessMemory()
  140. {
  141. return Process.GetCurrentProcess().PrivateMemorySize64;
  142. }
  143. #region 查找所有应用程序标题
  144. /// <summary>
  145. /// 查找所有应用程序标题
  146. /// </summary>
  147. /// <returns>应用程序标题范型</returns>
  148. public static List<string> FindAllApps(int Handle)
  149. {
  150. List<string> Apps = new List<string>();
  151. int hwCurr;
  152. hwCurr = GetWindow(Handle, GW_HWNDFIRST);
  153. while (hwCurr > 0)
  154. {
  155. int IsTask = (WS_VISIBLE | WS_BORDER);
  156. int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
  157. bool TaskWindow = ((lngStyle & IsTask) == IsTask);
  158. if (TaskWindow)
  159. {
  160. int length = GetWindowTextLength(new IntPtr(hwCurr));
  161. StringBuilder sb = new StringBuilder(2 * length + 1);
  162. GetWindowText(hwCurr, sb, sb.Capacity);
  163. string strTitle = sb.ToString();
  164. if (!string.IsNullOrEmpty(strTitle))
  165. {
  166. Apps.Add(strTitle);
  167. }
  168. }
  169. hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
  170. }
  171. return Apps;
  172. }
  173. #endregion
  174. }
  175. }