0

I am creating several process using code bellow,and I want to kill them all when their parent dies.

Process p = new Process();
p.StartInfo.FileName = @"G:\test.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.Start();

1- I searched to much but cant found a solution. the only usefull solution is :

Solution

but I have an error on this line Win32.CloseHandle(m_handle);

2- I am using .netframework 3.5 and I cant use tasks.

3- I used p.WaitForExit(); but my mainform freezed .

Is there any way to kill processes on parent dies without freezing the form ?

Community
  • 1
  • 1
Mahsa
  • 363
  • 4
  • 9
  • 24
  • thanks for your reply. I saw that post to but still have a problem with Win32.CloseHandle(m_handle); . it doesnt work on win7 – Mahsa Jul 24 '14 at 13:28
  • Just use SafeHandle instead. – Adriano Repetti Jul 24 '14 at 13:30
  • you mean "Microsoft.Win32.SafeHandles.SafeFileHandle"? In my working domain,I have win7 and winxp,(32 and 64 bit).would it be work on all of them? – Mahsa Jul 24 '14 at 13:54
  • On third comment of that post,Alexander Yezutov wrotes :"Unfortunately, I was unable to run this in 64bit mode. Here I posted a working example, which is based on this." and gave a solution : http://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net/9164742#9164742 . but I still have a problem with windows versions and Win32.CloseHandle(handle); – Mahsa Jul 24 '14 at 14:09

3 Answers3

1

Just kill the process when your application ends. You can register Form.Closing from your Main form application.

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Process [] localByName = Process.GetProcessesByName("yoursubprocess");
    foreach (var process in processes) 
    {
        process.Kill() ; 
    }
}

EDIT :

yoursubprocess : children process name you want to kill. Not the main process

Perfect28
  • 11,089
  • 3
  • 25
  • 45
1

From the child process name it looks like you are their author. Then, instead of killing process, you can use any form of IPC to simply tell children "please, shutdown".

Simplest example

// in main application
Mutex mutex = new Mutex(true, "MyMutexWithUniqueName");
... do job here
mutex.ReleaseMutex(); // on exit

// in children
Mutex mutex = Mutex.OpenExisting("MyMutexWithUniqueName");
if(mutex.WaitOne(0)) // poll
{
    mutex.ReleaseMutex();
    ... exit process
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

You can stock your process in a list. Each time you create one, add it in the list. Then on main process closing you just have to stop them one by one. Isn't it ?

antoinestv
  • 3,286
  • 2
  • 23
  • 39