Trapping process starting/ending?

  • Thread starter Thread starter JamesB
  • Start date Start date
J

JamesB

Hi,
I need to trap a particular process starting or ending. I have done this
using process.start from within my own app, but powers that be really want
it to be seperate (i.e. my app should actually be a service, and not require
the other process starting from within my own program).

Fair enough - what's the best way?

My initial thought was to scan through the list of processes running every
minute or so and looking for the one of interest, but this seems rather
resource hungry and kludgy. Is there any o/s event I can easily tap in to
for new / ended processes, and if so, are there any examples online?
Thanks
James
 
James,

You should use the EventQuery class in the System.Management namespace.
As a matter of fact, the "about" documentation for the class has an example
of how you can be notified when a process is started and stopped.

Mind you, using this method is going to be a resource hog, as it relies
on a polling mechanism, but there really is no other way to go about it.
 
Hi James,

I wrote something to do exactly this recently. Heres the code:


using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

class ProcessObserver : IDisposable
{
ManagementEventWatcher m_processStartEvent = null;
ManagementEventWatcher m_processStopEvent = null;

public ProcessObserver(string processName,
EventArrivedEventHandler onStart, EventArrivedEventHandler onStop)
{
WqlEventQuery startQuery = new
WqlEventQuery("Win32_ProcessStartTrace",
String.Format("ProcessName='{0}'", processName));
m_processStartEvent = new
ManagementEventWatcher(startQuery);

WqlEventQuery stopQuery = new
WqlEventQuery("Win32_ProcessStopTrace",
String.Format("ProcessName='{0}'", processName));
m_processStopEvent = new
ManagementEventWatcher(stopQuery);

if (onStart != null)
m_processStartEvent.EventArrived += onStart;

if (onStop != null)
m_processStopEvent.EventArrived += onStop;
}

public void Start()
{
m_processStartEvent.Start();
m_processStopEvent.Start();
}

public void Dispose()
{
m_processStartEvent.Dispose();
m_processStopEvent.Dispose();
}
}

-James
 
Nicholas Paldino said:
James,

You should use the EventQuery class in the System.Management namespace.
As a matter of fact, the "about" documentation for the class has an
example of how you can be notified when a process is started and stopped.

Mind you, using this method is going to be a resource hog, as it relies
on a polling mechanism, but there really is no other way to go about it.


Sure there are better ways, take a look at James reply.

Willy.
 
james said:
Hi James,

I wrote something to do exactly this recently. Heres the code:
<snip>

Thanks for all the replies - am picking this up again today, so I'll give
the code you supplied a try.
Cheers,
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top