简体   繁体   中英

C# close the sub application when it's parent application is closing

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");
    }
}

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?

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM