Other Posts in Multithreading

  1. Killing a Process through C#
  2. Multithreading in C#

Killing a Process through C#

12/1/2008

I hate testing my code (which is probably fairly evident by the quality on my site)... That being said, I love automating anything and everything. In doing so I needed something that would run an application automatically in the background and kill the application (specifically IE and Firefox as it was some basic web development testing). Finding code that opens up a webpage is rather simple to find so I wont go into that. The idea of killing the process though was a bit more difficult to find (not too much though). And if I had looked under System.Diagnostics I would have found the appropriate object immediately (Process).

The Process class can pretty much handle everything for you. You call one of its functions looking for a specific process, it returns it, you call Kill on the process, and we're done. However I needed this to be done on a timer (to ensure the page loaded completely) and asynchronously (as I needed the application to test multiple things at once and not all of them were going to be web based). Therefore I came up with a couple of functions to help me out... Well actually just two functions:

   1: /// <summary>
   2: /// Kills a process
   3: /// </summary>
   4: /// <param name="ProcessName">Name of the process without the ending (ie iexplore instead of iexplore.exe)</param>
   5: public static void KillProcess(string ProcessName)
   6: {
   7:     Process[] Processes = Process.GetProcessesByName(ProcessName);
   8:     foreach (Process CurrentProcess in Processes)
   9:     {
  10:         CurrentProcess.Kill();
  11:     }
  12: }
  13:  
  14: /// <summary>
  15: /// Kills a process after a specified amount of time
  16: /// </summary>
  17: /// <param name="ProcessName">Name of the process</param>
  18: /// <param name="TimeToKill">Amount of time (in ms) until the process is killed.</param>
  19: public static void KillProcess(string ProcessName, int TimeToKill)
  20: {
  21:     ThreadPool.QueueUserWorkItem(delegate { KillProcessAsync(ProcessName, TimeToKill); });
  22: }
  23:  
  24: /// <summary>
  25: /// Kills a process asyncronously
  26: /// </summary>
  27: /// <param name="ProcessName">Name of the process to kill</param>
  28: /// <param name="TimeToKill">Amount of time until the process is killed</param>
  29: private static void KillProcessAsync(string ProcessName, int TimeToKill)
  30: {
  31:     if (TimeToKill > 0)
  32:         Thread.Sleep(TimeToKill);
  33:     Process[] Processes = Process.GetProcessesByName(ProcessName);
  34:     foreach (Process CurrentProcess in Processes)
  35:     {
  36:         CurrentProcess.Kill();
  37:     }
  38:  
  39: }

There are two options. The first kills the process immediately. The second function on the other hand actually asks for the processes (after sleeping the appropriate amount of time) and kills them. It's very basic and commented so I don't believe it would take too much time to understand. But with this, I've been able to set my computer on auto pilot at night, gathering info for me to go over in the morning and ensure that I come back to a clean desktop. So I hope this helps you out as well. So try it out, leave feedback, and happy coding.



Comments