5

I have a C# winform application that during its work opens another Winform process. The other process has UI of its own. When I close the parent application, I want the other application to be closed automatically.

How can I achieve that?

Thanks

Ragoler
  • 1,261
  • 4
  • 15
  • 20
  • see this link http://stackoverflow.com/questions/169483/c-win32-notify-when-separate-window-is-closing-closed – almog.ori Jun 03 '09 at 08:45
  • That refers to a similar, but opposite, task (shutting down the current app when another window closes). The CodeProject article linked there should however be of some use. – Noldorin Jun 03 '09 at 08:55

3 Answers3

5

If you are using Process.Process there is the CloseMainWindow method. If you keep a reference to the object you can use it later.

Here's the relevant page in the MSDN

and the relevant code:

// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • I can't see the reason to downvote this one; it's a perfectly workable solution (even though I would add a call to CloseMainWindow before calling the Close method). – Fredrik Mörk Jun 03 '09 at 08:50
  • Down-voted for the same reason as Shoban's answer. This is not the right way to close a process. – Noldorin Jun 03 '09 at 08:53
  • OK so I got the wrong method to start with, but really reading the MSDN page gave the correct one - which I'm sure the OP is capable of doing – ChrisF Jun 03 '09 at 08:57
  • Down-vote removed. CloseMainWindow is actually the way to go. :) – Noldorin Jun 03 '09 at 08:57
  • Thanks - I should have double checked the help page myself ;) – ChrisF Jun 03 '09 at 08:58
3

There are several different options. I would suggest that you have your application keep track of the processes that it starts:

private Stack<Process> _startedProcesses = new Stack<Process>();
private void StartChildProcess(string fileName)
{
    Process newProcess = new Process();
    newProcess.StartInfo = new ProcessStartInfo(fileName); ;
    newProcess.Start();    
    _startedProcesses.Push(newProcess);
}

When the application closes, you can call a method that will close all started child processes that are still running. You can use this either with the Kill method or by calling the CloseMainWindow and Close methods. CloseMainWindow/Close will perform a more graceful close (if you start Notepad and there are unsaved changes, Kill will lose them, CloseMainWindow/Close will make notepad ask if you want to save):

private void CloseStartedProcesses()
{
    while (_startedProcesses.Count > 0)
    {
        Process process = _startedProcesses.Pop();
        if (process != null && !process.HasExited)
        {
            process.CloseMainWindow();
            process.Close();
        }
    }
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
1

The most graceful way to do this is probably to send a window message to the main from of the other process. You can get the handle of this main form simply using the Process.MainWindow.Handle property (I assume you are using the Process class, and then just use the PostMessage Win API call to send a message with a custom ID to the main window of this "child" process. Then, the message loop of the other process can easily detect this message (by overriding the WndProc method) and perform a proper shutdown accordingly. An alternative would be to send the standard WM_CLOSE method, which would mean you would just have to unload the application from the handler of the Form.Closed event, but may perhaps allow you less control (over whether to cancel the shutdown in certain situations).

Noldorin
  • 144,213
  • 56
  • 264
  • 302