473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trapping process starting/ending?

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

May 14 '07 #1
4 2528
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JamesB" <ja***@somewhere.com.net.com.netwrote in message
news:46***********************@news.zen.co.uk...
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

May 14 '07 #2
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

May 14 '07 #3
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
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.

May 16 '07 #4

"james" <ja********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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

May 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
7
by: Ryan Walker | last post by:
Hi, I'm getting started with python and have almost zero programming experience. I'm finding that there are tons of tutorials on the internet -- such as the standard tutorial at python.org -- that...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
10
by: shiry | last post by:
Hi, I need to do some important cleanup before my console application exists. I used the console ctrl event. This is working well and it fires for all cases, including the CTRL_CLOSE_EVENT (if I...
9
by: Eran.Yasso | last post by:
Hi, My app starts process. Some times this process exits because of exception. Can my app know if the process exited due to exception or gracefully? In both ways, the exit code of this...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
0
by: =?Utf-8?B?Um9i?= | last post by:
I've a requirement to monitor when certain applications are started. I'm using WMI called from VS Stusio 2005 in VB to trap Excel and Word starting. I've written the following console application...
2
by: liorjj | last post by:
Hi, I need your help for some sporadic problem that I got during starting new process (C# 1.1). when I 'm starting new process on remote machine, my process starts properly but from some...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.