2010年12月31日 星期五

監視一個處理程序

using System;
using System.Windows.Forms;

namespace DevDistrict.MachineMonitor
{
 public class Monitor
 {
  [STAThread()]
  public static void Main()
  {
   System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("Notepad");

   if (p.Length<0)
   {
    Console.WriteLine("No process");
    return;
   }

   if (p[0].HasExited)
   {
    Console.WriteLine("Process Exited");
    return;
   }

   p[0].Exited+=new EventHandler(Startup_Exited);

   while(!p[0].HasExited)
   {
    p[0].WaitForExit(30000);
    Console.WriteLine("checking process health");
   }
  }

  private static void Startup_Exited(object sender, EventArgs e)
  {
   Console.WriteLine("PROCESS EXIT CAUGHT");
  }
 }
}

沒有留言: