Click here to Skip to main content
15,891,529 members
Home / Discussions / C#
   

C#

 
GeneralRe: C#->C++ Interface Issue - Passing Data Pin
Heath Stewart23-Mar-04 9:04
protectorHeath Stewart23-Mar-04 9:04 
GeneralDetect new running process Pin
Apusnaias23-Mar-04 5:53
Apusnaias23-Mar-04 5:53 
GeneralRe: Detect new running process Pin
Dave Kreskowiak23-Mar-04 6:12
mveDave Kreskowiak23-Mar-04 6:12 
GeneralRe: Detect new running process Pin
Heath Stewart23-Mar-04 9:16
protectorHeath Stewart23-Mar-04 9:16 
GeneralRe: Detect new running process Pin
Apusnaias23-Mar-04 11:22
Apusnaias23-Mar-04 11:22 
GeneralRe: Detect new running process Pin
Dave Kreskowiak23-Mar-04 11:46
mveDave Kreskowiak23-Mar-04 11:46 
GeneralRe: Detect new running process Pin
Apusnaias23-Mar-04 23:30
Apusnaias23-Mar-04 23:30 
GeneralRe: Detect new running process Pin
ian mariano23-Mar-04 11:20
ian mariano23-Mar-04 11:20 
For the most basic monitoring, you can write a simple class to monitor processes:

//  C#, AS-IS, may need tweaking, minimal comments
 
using   System;
using   System.Collections;
using   System.Diagnostics;
using   System.Threading;
 
public delegate void   ProcessStartedEventHandler(object sender, Process proc);
 
public delegate void   ProcessExitedEventHandler(object sender, Process proc);
 
public class   ProcessWatcher
{
   #region   Constants
   //   scan at XXXXms intervals from last scan
   private const int  SCAN_INTERVAL = 250;
   #endregion   Constants
 
   #region   Instance Fields
   private Mutex   _myLock = new Mutex(false);
   private Hashtable   _procList = new Hashtable();
   private Timer   _scanTimer = null;
   #endregion   Instance Fields
 
   #region   Construction / Destruction
   public   ProcessWatcher()   {   }
 
   ~ ProcessWatcher()
   {
      Stop();
   }
   #endregion   Construction / Destruction
 
   #region   Methods
   public void   Start()
   {
      _myLock.WaitOne();
 
      //   sanity
      if (_scanTimer != null)
      {
         _myLock.ReleaseMutex();
         
         return;
      }
 
      _procList.Clear();
 
     //  kick off the scan
      _scanTimer = new Timer(new TimerCallback(_Scan), null,
         SCAN_INTERVAL, Timeout.Infinite);
 
      _myLock.ReleaseMutex();
   }
 
   public void   Stop()
   {
      _myLock.WaitOne();   //  wait for access
 
      //   sanity
      if (null == _scanTimer)
      {
         _scanMutex.ReleaseMutex();
         
         return;
      }
 
      _scanTimer.Dispose();
      _scanTimer = null;
 
      _procList.Clear();
 
      _myLock.ReleaseMutex();
   }
   #endregion   Methods
 
   #region   Implementation
   private void   _Scan(object unused)
   {
      _myLock.WaitOne();   //  wait for access
 
      //  prevent firing while working
      _scanTimer.Change(Timeout.Infinite, Timeout.Infinite);

      //  copy list of event subscribers AT THE TIME OF THIS SCAN
      //  the current subscribers will be copied by the assignment operator
      ProcessStartedEventHandler  startListeners = ProcessStarted;
      ProcessExitedEventHandler   exitListeners = ProcessExited;
 
      //   first we'll scan for those processes which ended
      ArrayList  list = new ArrayList();
      Process   cur = null;
 
      foreach (Process proc in _procList)
      {
         if (!proc.HasExited)  continue;
 
         list.Add(proc.Id);
      }
 
      //  fire off exit notifications and remove
      foreach (int id in list)
      {
         if (exitListeners != null)
            exitListeners(this, (Process)_procList[id]));
 
         _procList.Remove(id);
      }
      
      list.Clear();
 
      //   get current processes
      Process[]  running = Process.GetProcesses();
 
      foreach (Process proc in running)
      {
         if (_procList.Contains(proc.Id))   continue;
 
         list.Add(proc);   //  got a new one!
      }
 
      //  fire off notifications and add
      foreach (Process proc in list)
      {
         _procList.Add(proc.Id, proc);
 
         if (startListeners != null)
            startListeners(this, proc); 
      }
 
      //  setup next call and release mutex
      _scanTimer.Change(SCAN_INTERVAL, Timeout.Infinite);
      _myLock.ReleaseMutex();
   }
   #endregion   Implementation
 
   #region   Events
   public event ProcessStartedEventHandler   ProcessStarted;
   public event ProcessExitedEventHandler   ProcessExited;
   #endregion   Events
}


With the above, you can add handlers to the ProcessStarted and ProcessExited events to do whatever you wish.

The class is very basic and just off the top of my head. It may have unforseen issues so be cautious! It should be fairly thread-safe, with the _myLock.WaitOne and _myLock.ReleaseMutex calls to lock and unlock access to the important bits, but you may want to look for deadlock conditions.

You may need to adjust the SCAN_INTERVAL to better suit your needs, though I don't recommend really small intervals.

All you need to do is write your particular ProcessStarted and ProcessExited handlers; create an instance field of the class; add the hanlers to the appropriate events; and call Start to begin monitoring. The destructor will automatically call Stop for you.

Enjoy!

Ian Mariano - http://www.ian-space.com/


"We are all wave equations in the information matrix of the universe" - me

GeneralRe: Detect new running process Pin
Robert M Greene26-Oct-04 7:39
Robert M Greene26-Oct-04 7:39 
GeneralGraphics.MeasureString Pin
compbssn200323-Mar-04 5:30
compbssn200323-Mar-04 5:30 
GeneralRe: Graphics.MeasureString Pin
Heath Stewart23-Mar-04 5:33
protectorHeath Stewart23-Mar-04 5:33 
GeneralRe: Graphics.MeasureString Pin
leppie23-Mar-04 6:06
leppie23-Mar-04 6:06 
GeneralRe: Graphics.MeasureString Pin
compbssn200323-Mar-04 19:20
compbssn200323-Mar-04 19:20 
GeneralExposing constituent control properties in VS.NET Pin
Matt Davison23-Mar-04 5:07
Matt Davison23-Mar-04 5:07 
GeneralRe: Exposing constituent control properties in VS.NET Pin
Heath Stewart23-Mar-04 5:30
protectorHeath Stewart23-Mar-04 5:30 
GeneralRe: Exposing constituent control properties in VS.NET Pin
Heath Stewart23-Mar-04 8:16
protectorHeath Stewart23-Mar-04 8:16 
QuestionHow can I do the following... Pin
profoundwhispers23-Mar-04 4:36
profoundwhispers23-Mar-04 4:36 
AnswerRe: How can I do the following... Pin
Heath Stewart23-Mar-04 4:54
protectorHeath Stewart23-Mar-04 4:54 
GeneralRe: How can I do the following... Pin
profoundwhispers23-Mar-04 5:08
profoundwhispers23-Mar-04 5:08 
GeneralRe: How can I do the following... Pin
Heath Stewart23-Mar-04 5:26
protectorHeath Stewart23-Mar-04 5:26 
Generalinput a barecode Pin
tonaxxl23-Mar-04 4:18
tonaxxl23-Mar-04 4:18 
GeneralRe: input a barecode Pin
Dave Kreskowiak23-Mar-04 4:26
mveDave Kreskowiak23-Mar-04 4:26 
GeneralRe: input a barecode Pin
tonaxxl23-Mar-04 4:35
tonaxxl23-Mar-04 4:35 
GeneralRe: input a barecode Pin
Dave Kreskowiak23-Mar-04 5:24
mveDave Kreskowiak23-Mar-04 5:24 
GeneralRe: input a barecode Pin
tonaxxl23-Mar-04 16:42
tonaxxl23-Mar-04 16:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.