2

I want to close the sub application (which is fileViewer:Window) when it's parrent application would be closed.

Note: this parrent application is not the Main application.

It means I have C# winform application which have 3 levels.

1st is Main application 2nd is Sub application of Main application 3rd is sub application of 2nd

When I close the 2nd application, I want the 3rd application will be closed automatically.

This thread Close another process when application is closing is quite helpfull but I don't know where to call process.close() or fileViewer.FileBrowser.Dispose(); etc method

public void OpenNewInstanceOf2nd()
{
    //Use .NET Diagnostics to start a new process off the 2nd
    try
    {
        System.Diagnostics.Process.Start(MyFramework.FileSystem.ApplicationPath + MyProperties.MyAssemblyName);
    }
    catch (Exception ex)
    {
        MyFramework.Debug.LogException(ex, "Could not open the My Utility");
    }
}
Community
  • 1
  • 1
Khokhar
  • 685
  • 1
  • 9
  • 22
  • What link? Looks like you deleted it by accident. – Polynomial Nov 21 '11 at 11:05
  • Do you mean to shutdown a "worker" process that the parent application is closing? For this you would need some form of Inter-process Communications (lots of options, what is best: need more context). Otherwise please revise question for clarity. – Richard Nov 21 '11 at 11:06
  • Have you developed these application as forms which is part of same process or exe? Or are you starting different process at each level? – Amit Rai Sharma Nov 21 '11 at 11:37
  • @Richard: When I press the cross [X] button then application is closing. I want to find that point where i could write my code. Acutally this is a very big application and I am new to working on it. – Khokhar Nov 21 '11 at 11:44
  • @Polynomial: please check now, link is working. Just guide me where shold i call dispose() method so that sub application will be closed automatically. – Khokhar Nov 21 '11 at 11:47

2 Answers2

3

If its just a Form you want to close, you can set the owner of Form 3rd to Form 2nd. If you close Form 2nd, Form 3rd will be closed as well.

private void Form2nd_Load(object sender, EventArgs e)
{
  Form3rd form_3rd = new Form3rd();
  form_3rd.Show(this);
}

If its a nested process / worker thread, why don't you just keep a reference in the 2nd form/application and dispose it if your 2nd app closes?

Alex
  • 7,901
  • 1
  • 41
  • 56
  • I want to dispose it, but this is a very big application which is not developed by me. So, i am unable to find the point where should i call dispose(). – Khokhar Nov 21 '11 at 11:40
  • @Khokhar - If you have access to the code your going to have to figure it out. We cannot tell you. – Security Hound Nov 21 '11 at 12:05
2

If i understand correctly your question, you have 3 different processes where the last two were created by the previous:

Proc1 > Proc2 > Proc3

The first thing i want to ask you is if you really need such setup. If everything is under your control you should consider making a single process, but if for some reason you really need to do this (i.e. you need to run other command line utilities) lets move on...

If you create the "child" processes in your code you can keep the reference:

Process proc2 = Process.Start("proc2.exe");

and then close the process:

proc2.CloseMainWindow();
proc2.Close();

If you don't have control on Proc2 you can get a reference to Proc3 from Proc1 using

Process.GetProcessesByName("proc3.exe");

Keep in mind that Process.CloseMainWindow() works only with apps which have a window, it's useless for command line utilities. Controlling other third-party process may be tricky, it really depends on how they're implemented, so i can't be more specific.


After the details in the comment i would add the following: if you start Proc2 from your own code you can have a reference to its Process object, so you can register to the Exited event and at that point search and close Proc3:

proc2 = Process.Start("HelpViewer.exe");
proc2.Exited += new EventHandler(proc2_Exited);
proc2.EnableRaisingEvents = true;


void proc2_Exited(object sender, EventArgs e)
{
    //Unregister the event and free the object resources
    proc2.Exited -= new EventHandler(proc2_Exited);
    proc2.Close();

    //Search and close the other process(es)
    Process[] processes = Process.GetProcessesByName("HelpViewer.exe");
    foreach(Process process in processes)
        process.CloseMainWindow();
}

If you don't start Proc2 from your code than there is no easy solution.

Zmaster
  • 1,095
  • 9
  • 23
  • 2nd and 3rd applications are actually the same process, which is shown in taskmanager. 3rd is just a help file viewer, which is opened through clicking on hyper link of 2nd. When i close 2nd by clicking Cross [X] button then i want to close 3rd one as well. But i don't know where shold i write this dispose() code. this is a big application, and difficult to find the exact place to write the code. – Khokhar Nov 21 '11 at 12:01
  • If you have no other information but the process name and it is an external application with no interfaces you could acces, you might as well use FindWindow / TerminateProcess of the Win32 API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx – Alex Nov 21 '11 at 12:34
  • I updated my answer after your comment, which gives more details. – Zmaster Nov 21 '11 at 13:02