如何创建比其父进程寿命更长的进程

发布于 2024-07-24 14:07:34 字数 528 浏览 6 评论 0原文

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

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

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

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

编辑:

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

下面的答案都是正确的。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澉约 2024-07-31 14:07:34

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

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

using System.Diagnostics;

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

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

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");
    }
}
酒几许 2024-07-31 14:07:34

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

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

例如:

Launcher.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:

using System;
using System.Threading;

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

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

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

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

For example:

Launcher.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:

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.

睫毛溺水了 2024-07-31 14:07:34

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

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

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

希望你一切顺利。

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).

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.

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.

Hope all works out well for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文