title: C#.NET Monitoring for a Process
body: <p>We have this 3rd party in house app that's a little buggy and for now, till it's fixed, in our Citrix environment, I need to keep an eye on it and kill the process if it runs too long. I was able to poll for it and kill it if it was running but that's quite dirty and would require me to use a scheduled task.  I want a service to monitor and detect it then kill it if it's running too long.</p>
  
  <p>So I started a Windows Service project in Visual Studio and I found <a href="http://www.codeproject.com/Articles/12138/Process-Information-and-Notifications-using-WMI" rel="nofollow">this code from CodeProject</a> which registers with WMI using ManagementEventWatcher:</p>
  
  <pre><code>        string pol = "2";
          string appName = "MyApplicationName";
  
          string queryString =
              "SELECT *" +
              "  FROM __InstanceOperationEvent " +
              "WITHIN  " + pol +
              " WHERE TargetInstance ISA 'Win32_Process' " +
              "   AND TargetInstance.Name = '" + appName + "'";
  
          // You could replace the dot by a machine name to watch to that machine
          string scope = @"\\.\root\CIMV2";
  
          // create the watcher and start to listen
          ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
          watcher.EventArrived += new EventArrivedEventHandler(this.OnEventArrived);
          watcher.Start();
  </code></pre>
  
  <p>The problem with this code is that where it says "this.OnEventArrived", I get the following error:</p>
  
  <p>Error   1   'MyServiceApp.Service1' does not contain a definition for 'OnEventArrived' and no extension method 'OnEventArrived' accepting a first argument of type 'MyServiceApp.Service1' could be found (are you missing a using directive or an assembly reference?)</p>
  
  <p>What's the deal?</p>