C# 如何创建一个比其父进程寿命更长的进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1008886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:26:03  来源:igfitidea点击:

How to create a Process that outlives its parent

c#windowsprocess

提问by Chris Carter

I'm trying to launch an external updater application for a platform that I've developed. The reason I'd like to launch this updater is because my configuration utility which handles updates and license configuration for the platform has shared dependencies with other assemblies in the folder where the update will be deployed. So, while I can rename the configuration utility and overwrite it when deploying the update, I can't rename or overwrite the DLLs it depends on. Hence, the external updater application.

我正在尝试为我开发的平台启动外部更新程序应用程序。我想启动这个更新程序的原因是我的配置实用程序处理平台的更新和许可证配置,它与将部署更新的文件夹中的其他程序集共享依赖项。因此,虽然我可以重命名配置实用程序并在部署更新时覆盖它,但我无法重命名或覆盖它所依赖的 DLL。因此,外部更新程序应用程序。

I'm handling all of the update gathering logic in the configuration utility, then attempting to launch the updater to handle the actual file copy/overwrite operations. Obviously, because of the file in use issues, I need the configuration utility to exit right after the updater begins.

我正在处理配置实用程序中的所有更新收集逻辑,然后尝试启动更新程序来处理实际的文件复制/覆盖操作。显然,由于文件使用问题,我需要配置实用程序在更新程序开始后立即退出。

The problem I'm having is that I'm using the standard Process.Start method of launching the updater, and as soon as the configuration utility exits, the updater process gets killed too.

我遇到的问题是我使用标准 Process.Start 方法启动更新程序,一旦配置实用程序退出,更新程序进程也会被终止。

Is there any way that I can create a Process that outlives its parent, or launch an external application that can run beyond the program that launched it?

有什么方法可以创建一个比其父进程寿命更长的进程,或者启动一个可以在启动它的程序之外运行的外部应用程序?

EDIT:

编辑:

Apparently, in my updater application, I miscalculated the number of command line arguments which are passed to it. Because of this, the updater would exit immediately. I misinterpreted this to mean that the launcher application was killing the "child" process, when in fact, it wasn't.

显然,在我的更新程序应用程序中,我错误地计算了传递给它的命令行参数的数量。因此,更新程序将立即退出。我误解了这意味着启动器应用程序正在杀死“子”进程,而实际上并非如此。

The answers below are correct.

下面的答案是正确的。

采纳答案by Dirk Vollmar

It seems that the problem you are seeing has a different reason because the Processclass will not kill any processes started using Process.Startwhen your application exits.

您看到的问题似乎有不同的原因,因为Process该类不会终止Process.Start在您的应用程序退出时开始使用的任何进程。

See this simple sample program, the calculator will stay open:

看到这个简单的示例程序,计算器将保持打开状态:

using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        Process.Start(@"C:\windows\system32\calc.exe");
    }
}

回答by Jon Skeet

There's no reason why a process started with Process.Startshould automatically die when the launcher exits. My guess is that you're doing something odd in the updater.

Process.Start当启动器退出时,没有理由启动的进程应该自动终止。我的猜测是你在更新程序中做了一些奇怪的事情。

I've written an updater doing exactly this kind of thing before, and it's been fine.

我以前写过一个更新程序来做这种事情,而且很好。

For example:

例如:

Launcher.cs:

启动器.cs:

using System;
using System.Diagnostics;

class Launcher
{
    static void Main()
    {
        Console.WriteLine("Launching launchee");
        Process.Start("Launchee.exe");
        Console.WriteLine("Launched. Exiting");
    }
}

Launchee.cs:

Launchee.cs:

using System;
using System.Threading;

class Launchee
{
    static void Main()
    {
        Console.WriteLine("       I've been launched!");
        Thread.Sleep(5000);
        Console.WriteLine("       Exiting...");
    }
}

Compile both of them, separately, and run Launcher.exe. The "launchee" process definitely lasts longer than the launcher.

分别编译它们,然后运行 ​​Launcher.exe。“launchee”进程肯定比启动器持续更长时间。

回答by James Conigliaro

Just a thought from my foggy memory, but I seem to remember having a discussion a while back that when the Process.Start method is called from Form that the spawned process has some sort of dependency (not sure what, why or how, memory is a bit foggy).

只是我模糊的记忆中的一个想法,但我似乎记得不久前有一个讨论,当从 Form 调用 Process.Start 方法时,产生的进程具有某种依赖性(不确定是什么,为什么或如何,内存是有点雾)。

To deal with it, a flag was set that was actually called from the Main() method of the application after the main form/app exited and that if the process was launched from the Main() method, eveything worked out just fine.

为了解决这个问题,设置了一个标志,在主窗体/应用程序退出后,该标志实际上是从应用程序的 Main() 方法调用的,如果该进程是从 Main() 方法启动的,则一切正常。

Just a thought, like I said, this is purely from memory, but some of the examples posted here all being called from the Main() method of a console app seemed to jog something.

只是一个想法,就像我说的,这纯粹是出于记忆,但是这里发布的一些示例都是从控制台应用程序的 Main() 方法调用的,似乎在慢跑。

Hope all works out well for you.

希望一切都对你有好处。