Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
9.6 KiB

  1. namespace WindowsApplication1
  2. {
  3. using System;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ServiceProcess;
  7. using System.Diagnostics;
  8. using System.DirectoryServices;
  9. using Microsoft.Win32.Diagnostics; /* Instrumentation */
  10. // Summary description for ProcessControllerManager.
  11. // This class is used to handle all the processes on a machine
  12. public class ProcessControllerManager
  13. {
  14. private string strMachineName;
  15. private ListBox lstPcsRun, lstPcs,lstCurrent=null;
  16. private string strSelectedProcess="";
  17. private Process pcsSelectedProcess;
  18. private System.Collections.Hashtable colProcesses=new System.Collections.Hashtable();
  19. private System.Windows.Forms.Timer tmrWatchDog = new System.Windows.Forms.Timer();
  20. private TraceProvider MyTraceProvider = new TraceProvider("ProcessController",new Guid("{C5EBCA17-E93F-4733-865B-DEC4039ADB6D}"));
  21. public ProcessControllerManager()
  22. {
  23. //Default constructor. Don't need any code
  24. }
  25. //Clear all the collections
  26. public void Clear()
  27. {
  28. strMachineName="";
  29. if(lstPcsRun!=null)
  30. {
  31. lstPcsRun.Items.Clear();
  32. lstPcsRun.SelectedIndexChanged -=new System.EventHandler(this.SelectedProcess);
  33. lstPcsRun.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.ListOptions);
  34. }
  35. if(lstPcs!=null)
  36. {
  37. lstPcs.Items.Clear();
  38. lstPcs.SelectedIndexChanged -= new System.EventHandler(this.SelectedProcess);
  39. lstPcs.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.ListOptions);
  40. }
  41. colProcesses.Clear();
  42. }
  43. //Class explicit constructor
  44. public ProcessControllerManager(ListBox tmpPcsRun, ListBox tmpPcs, string tmpMachineName )
  45. {
  46. strMachineName=tmpMachineName;
  47. colProcesses.Clear();
  48. //Adding the events handlers to the controls
  49. lstPcs=tmpPcs;
  50. lstPcs.SelectedIndexChanged += new System.EventHandler(this.SelectedProcess);
  51. lstPcs.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListOptions);
  52. lstPcsRun=tmpPcsRun;
  53. lstPcsRun.SelectedIndexChanged += new System.EventHandler(this.SelectedProcess);
  54. lstPcsRun.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListOptions);
  55. LoadProcesses();
  56. }
  57. //Launch the selected process
  58. public void StartProcess(string strProcName)
  59. {
  60. Process tmpProcess = new Process();
  61. tmpProcess.Exited += new EventHandler(this.ProcessExited);
  62. tmpProcess.EnableRaisingEvents=true;
  63. tmpProcess.StartInfo.FileName=strProcName;
  64. try
  65. {
  66. tmpProcess.Start();
  67. while(! tmpProcess.Responding)System.Windows.Forms.Application.DoEvents();
  68. LoadProcesses();
  69. }
  70. catch
  71. {
  72. MessageBox.Show("The Process: " + strSelectedProcess + " cannot start !");
  73. }
  74. }
  75. //Add/remove options from the popup menu
  76. private void ListOptions( object sender, System.Windows.Forms.MouseEventArgs e)
  77. {
  78. lstCurrent=(ListBox)sender;
  79. if(e.Button==System.Windows.Forms.MouseButtons.Right && lstCurrent.Equals(lstPcsRun))
  80. {
  81. lstCurrent = (ListBox)sender;
  82. lstCurrent.ContextMenu=new System.Windows.Forms.ContextMenu();
  83. if(lstCurrent.Equals(lstPcs))
  84. {
  85. strSelectedProcess=lstCurrent.SelectedItem.ToString();
  86. }
  87. else
  88. {
  89. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("&Terminate Process", new EventHandler(this.KillProcess)));
  90. }
  91. lstCurrent.ContextMenu.Show(lstCurrent ,new Point(e.X,e.Y));
  92. }
  93. }
  94. //Try to stop a process
  95. private void KillProcess( object sender, EventArgs e)
  96. {
  97. try
  98. {
  99. string strProcName = lstCurrent.SelectedItem.ToString();
  100. if(pcsSelectedProcess!=null)
  101. {
  102. try
  103. {
  104. //Try to terminate the process
  105. pcsSelectedProcess.Kill();
  106. if(colProcesses.Contains(pcsSelectedProcess.Id.ToString()))
  107. {
  108. colProcesses.Remove(pcsSelectedProcess.Id.ToString());
  109. lstPcsRun.Items.Remove (pcsSelectedProcess.ProcessName + " ID: " + pcsSelectedProcess.Id.ToString());
  110. }
  111. }
  112. catch
  113. {
  114. MessageBox.Show(pcsSelectedProcess.ProcessName + " can not be killed");
  115. }
  116. }
  117. lstPcs.Items.Clear();
  118. }
  119. catch
  120. {
  121. MessageBox.Show("Select a process first!") ;//no listItem was selected
  122. }
  123. }
  124. //Fills up the Process Info list
  125. private void ShowProcessInfo( object sender, EventArgs e)
  126. {
  127. string strItem = lstCurrent.SelectedItem.ToString();
  128. string strKey = strItem.Substring(strItem.IndexOf("ID:")+3).Trim();
  129. Process tmpProcess=null;
  130. if(strKey !="" )
  131. {
  132. try
  133. {
  134. tmpProcess= Process.GetProcessById(Int32.Parse (strKey),strMachineName);
  135. }
  136. catch
  137. {
  138. Process[] arrProcess=Process.GetProcesses(strMachineName);
  139. foreach(Process tmpP in arrProcess)
  140. {
  141. if(tmpP.Id==Int32.Parse(strKey))
  142. {
  143. tmpProcess=tmpP;
  144. break;
  145. }
  146. }
  147. }
  148. lstPcs.Items.Clear();
  149. //Adding the Process info to the list
  150. try
  151. {
  152. lstPcs.Items.Add("Process Name: " + tmpProcess.ProcessName );
  153. lstPcs.Items.Add("Arguments: " + tmpProcess.StartInfo.Arguments);
  154. lstPcs.Items.Add("Running on: " + tmpProcess.MachineName);
  155. try
  156. {
  157. lstPcs.Items.Add("Main Window title: " + tmpProcess.MainWindowTitle);
  158. }
  159. catch
  160. {
  161. lstPcs.Items.Add("Main Window title: Not Available");
  162. }
  163. lstPcs.Items.Add("Start Time: " + tmpProcess.StartTime.ToString());
  164. lstPcs.Items.Add("NonpagedSystemMemorySize: " + tmpProcess.NonpagedSystemMemorySize.ToString());
  165. lstPcs.Items.Add("PagedMemorySize: " + tmpProcess.PagedMemorySize.ToString());
  166. lstPcs.Items.Add("PrivateMemorySize: " + tmpProcess.PrivateMemorySize.ToString());
  167. lstPcs.Items.Add("PrivilegedProcessorTime: " + tmpProcess.PrivilegedProcessorTime.ToString());
  168. lstPcs.Items.Add("PeakPagedMemorySize: " + tmpProcess.PeakPagedMemorySize.ToString());
  169. lstPcs.Items.Add("PeakVirtualMemorySize: " + tmpProcess.PeakVirtualMemorySize.ToString());
  170. lstPcs.Items.Add("PeakPagedMemorySize: " + tmpProcess.PeakPagedMemorySize.ToString());
  171. lstPcs.Items.Add("PeakWorkingSet: " + tmpProcess.PeakWorkingSet.ToString());
  172. try
  173. {
  174. lstPcs.Items.Add("PriorityClass: " + tmpProcess.PriorityClass.ToString());
  175. }
  176. catch
  177. {
  178. lstPcs.Items.Add("PriorityClass: Not available" );
  179. }
  180. try
  181. {
  182. lstPcs.Items.Add("BasePriority: "+ tmpProcess.BasePriority.ToString());
  183. }
  184. catch
  185. {
  186. lstPcs.Items.Add("BasePriority: Not available" );
  187. }
  188. try
  189. {
  190. lstPcs.Items.Add("ProcessorAffinity: " + tmpProcess.ProcessorAffinity.ToString());
  191. }
  192. catch
  193. {
  194. lstPcs.Items.Add("ProcessorAffinity: Not available");
  195. }
  196. lstPcs.Items.Add("ID: " + tmpProcess.Id);
  197. lstPcs.Items.Add("WorkingDirectory: " + tmpProcess.StartInfo.WorkingDirectory);
  198. lstPcs.Items.Add("");
  199. lstPcs.Items.Add("********************* Modules in use by this process ***************** ");
  200. lstPcs.Items.Add("");
  201. foreach(System.Diagnostics.ProcessModule tmpPM in tmpProcess.Modules)
  202. {
  203. try
  204. {
  205. lstPcs.Items.Add("Module Name: " + tmpPM.FileName );
  206. }
  207. catch
  208. {
  209. lstPcs.Items.Add("Modules Reading Not Allowed");
  210. }
  211. }
  212. }
  213. catch
  214. {
  215. MessageBox.Show("Some process properties couldn't be loaded!");//Some did not work
  216. }
  217. }
  218. }
  219. //Remove the process from the processes list
  220. private void ProcessExited(object sender, EventArgs e)
  221. {
  222. Process tmpProcess = (Process)sender;
  223. colProcesses.Remove(tmpProcess.Id.ToString());
  224. lstPcsRun.Items.Remove(tmpProcess.ProcessName + " ID: " + tmpProcess.Id.ToString());
  225. }
  226. //Check the selected process
  227. private void SelectedProcess(object sender, EventArgs e)
  228. {
  229. lstCurrent=(ListBox)sender;
  230. strSelectedProcess=lstCurrent.SelectedItem.ToString();
  231. if(lstCurrent.Equals(lstPcsRun))
  232. {
  233. string pcsID = lstCurrent.SelectedItem.ToString();
  234. pcsID= pcsID.Substring(pcsID.IndexOf("ID: ")+4).Trim();
  235. pcsSelectedProcess=(Process)colProcesses[pcsID];
  236. ShowProcessInfo(sender,e);
  237. }
  238. }
  239. //Load all the processes on the given machine
  240. private void LoadProcesses()
  241. {
  242. tmrWatchDog.Enabled=false;
  243. lstPcsRun.Items.Clear();
  244. colProcesses.Clear();
  245. colProcesses=new System.Collections.Hashtable();
  246. try
  247. {
  248. //Use the Static: GetProcesses to have the array of currently running processes.
  249. Process[]arrProcess=Process.GetProcesses(strMachineName);
  250. foreach(Process tmpPcs in arrProcess)
  251. {
  252. //Assign ProcessExited event to each process in the list
  253. tmpPcs.Exited += new EventHandler(ProcessExited);
  254. if(!colProcesses.Contains(tmpPcs.Id.ToString()) )
  255. {
  256. MyTraceProvider.TraceMessage((uint)TraceFlags.Info, "[{0}]Loading process {1} ID: {2}", strMachineName, tmpPcs.ProcessName, tmpPcs.Id.ToString()); /* Instrumentation */
  257. lstPcsRun.Items.Add(tmpPcs.ProcessName + " ID: " + tmpPcs.Id.ToString());
  258. colProcesses.Add(tmpPcs.Id.ToString(),tmpPcs);
  259. }
  260. }
  261. }
  262. catch
  263. {
  264. MessageBox.Show("Cannot read processes on: " + strMachineName );
  265. }
  266. //Enable the RaisingEvents for each process
  267. foreach(Process tmpPcs in colProcesses.Values)
  268. {
  269. try
  270. {
  271. tmpPcs.EnableRaisingEvents=true;
  272. }
  273. catch
  274. {
  275. Console.WriteLine("Couldn't Set Option");
  276. }
  277. }
  278. tmrWatchDog.Enabled=true;
  279. }
  280. }
  281. }