Terminate a child process when parent process is killed

G

Guest

Hello,

I'm using a C# application to create a process "A" to execute an .exe file.
The problem is that when the application is killed (for example from task
manager) the child process keeps on running.
The question is how can i run the "A" process so that when my application is
killed or crashes the child process "A" will be terminated?
I'm using this code:

Process A = new Process();
A.StartInfo.FileName = some_exe_path;
A.Start();

Thanks in advance
 
S

Sheng Jiang[MVP]

If you have source code access to the exe file, check the parent process
periodically in the exe and exit if the parent process is killed. If you
don't, write a loader process to do the same. Again, this only works if you
loader is not crashed or killed.
 
G

Guest

Hi Sheng,
Thanks for the response.
Unfortunately i don't have an access to the exe source code. However i think
i'll implement a loader class as you suggested. Just one request please. Can
you please supply a code snippet that describes how to check the parent
process responsiveness? - do you refer to .net remoting or do you suggest
another way?

Thanks again!
 
S

Sheng Jiang[MVP]

It is hard to define responsiveness for some applications. For example, you
can expect an IE window to stop responding for a while when a FTP address is
entered. Since there is no apparent main window for IE, the IE process can
be described as partially hang if another window is open and responding to
user input.

If your process has only one main window, use SendMessageTimeout to send a
WM_NULL message or check the Responding property of the Process object
returned by the Process.GetProcessById method.

You can pass your process id in the command line and parse the command line
in your loader process.

Reference

How to detect if an application has stopped responding by using Visual C#
http://support.microsoft.com/kb/304991
 

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

Top