简体   繁体   English

C#在父应用程序关闭时关闭子应用程序

[英]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. 我想在关闭子应用程序时关闭子应用程序(即fileViewer:Window)。

Note: this parrent application is not the Main application. 注意:该上级应用程序不是主应用程序。

It means I have C# winform application which have 3 levels. 这意味着我有3个级别的C#winform应用程序。

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(); 当应用程序关闭时,该线程关闭另一个进程 fileViewer.FileBrowser.Dispose(); ,但是我不知道在哪里调用process.close()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. 如果只是要关闭的表单,则可以将表单3rd的所有者设置为表单2nd。 If you close Form 2nd, Form 3rd will be closed as well. 如果您关闭Form 2nd,那么Form 3rd也将关闭。

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: 如果我正确理解您的问题,那么您有3个不同的过程,其中最后两个过程是由前一个过程创建的:

Proc1 > Proc2 > Proc3 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 如果您无法控制Proc2,则可以使用以下命令从Proc1获取对Proc3的引用

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. 请记住,Process.CloseMainWindow()仅适用于具有窗口的应用程序,它对命令行实用程序没有用。 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对象,因此可以注册到Exited事件,然后搜索并关闭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. 如果您不从代码中启动Proc2,那么没有简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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