父进程与子进程关系

发布于 2024-09-11 22:03:25 字数 316 浏览 4 评论 0 原文

我有打开子进程的父进程。仅当父进程不再运行时,我才需要执行某些功能。

知道父进程没有运行的最佳方法是什么?因为它可以被暴力终止,所以我不想创建一些在关闭事件时向我的子进程发送信号的功能。

或者只是像这样查找我的父进程:

在父进程中进行此操作并将其传递给子进程 Process.GetCurrentProcess().Id 并在孩子每隔几毫秒检查一次这个

Process localById = Process.GetProcessById(1234);

有什么想法吗?建议..

I have parent process that opens child process . I need to perform some functionality only when the parent process is no more running.

What is the best way to know that the parent process is not running ? Because it can be terminated violently then I don't want to make some functionality that will send signal to my child process on the closing event.

Or just looking for my parent process like that:

In the parent make this and pass it to the child Process.GetCurrentProcess().Id
And in the child every several milliseconds check this one

Process localById = Process.GetProcessById(1234);

Any ideas ? Recommendations ..

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

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

发布评论

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

评论(4

三生池水覆流年 2024-09-18 22:03:25

下面是一个如何使用 Process.WaitForExit 来检查 id 已在命令行上传递的父进程的简单示例:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static AutoResetEvent _autoResetEvent;

    static void Main(string[] args)
    {
        int parentProcessId = int.Parse(args[0]);

        _autoResetEvent = new AutoResetEvent(false);

        WaitCallback callback = delegate(object processId) { CheckProcess((int)processId); };
        ThreadPool.QueueUserWorkItem(callback, parentProcessId);

        _autoResetEvent.WaitOne();
    }

    static void CheckProcess(int processId)
    {
        try
        {
            Process process = Process.GetProcessById(processId);
            process.WaitForExit();
            Console.WriteLine("Process [{0}] exited.", processId);
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Process [{0}] not running.", processId);
        }

        _autoResetEvent.Set();
    }
}

可以使用 Process.Exited 事件来完成像这样:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static AutoResetEvent _autoResetEvent;

    static void Main(string[] args)
    {
        int parentProcessId = int.Parse(args[0]);
        Process process = Process.GetProcessById(parentProcessId);
        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(process_Exited);

        _autoResetEvent = new AutoResetEvent(false);
        _autoResetEvent.WaitOne();
    }

    static void process_Exited(object sender, EventArgs e)
    {
        Console.WriteLine("Process exit event triggered.");
        _autoResetEvent.Set();
    }
}

请注意,在两个示例中,AutoResetEvent 的目的只是为了防止主线程退出。在 Windows 窗体应用程序中,您不需要使用它,因为您的程序将处于消息循环中,只有在关闭它时才会退出。

Here is a simple example how to use Process.WaitForExit to check for a parent process whose id has been passed on the command line:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static AutoResetEvent _autoResetEvent;

    static void Main(string[] args)
    {
        int parentProcessId = int.Parse(args[0]);

        _autoResetEvent = new AutoResetEvent(false);

        WaitCallback callback = delegate(object processId) { CheckProcess((int)processId); };
        ThreadPool.QueueUserWorkItem(callback, parentProcessId);

        _autoResetEvent.WaitOne();
    }

    static void CheckProcess(int processId)
    {
        try
        {
            Process process = Process.GetProcessById(processId);
            process.WaitForExit();
            Console.WriteLine("Process [{0}] exited.", processId);
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Process [{0}] not running.", processId);
        }

        _autoResetEvent.Set();
    }
}

Using the Process.Exited event could be done like this:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static AutoResetEvent _autoResetEvent;

    static void Main(string[] args)
    {
        int parentProcessId = int.Parse(args[0]);
        Process process = Process.GetProcessById(parentProcessId);
        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(process_Exited);

        _autoResetEvent = new AutoResetEvent(false);
        _autoResetEvent.WaitOne();
    }

    static void process_Exited(object sender, EventArgs e)
    {
        Console.WriteLine("Process exit event triggered.");
        _autoResetEvent.Set();
    }
}

Note that in both samples the purpose of the AutoResetEvent is solely to prevent your main thread from exiting. In a Windows Forms application you would not need to use it as your program will be in a message loop and only exit if you close it.

寻找一个思念的角度 2024-09-18 22:03:25

底层 Win32 进程句柄是可等待的,并且当进程退出时将收到信号。

在本机代码中:

DWORD res = WaitForSIngleObject(hProcess, INFINITE);
if (res == WAIT_OBJECT_0) {
  // process has exited.
}

在本机代码中,您需要创建 WaitHandle 用于进程句柄,或使用 P/Invoke。 P/Invoke 的缺点是比较难以组合(使用 WaitForMultipleObjects 多次等待,因此您不会专门使用一个线程来等待一件事)。


感谢 0xA3:只需使用 Process.WaitForExit (存在超时重载以避免无限期等待,并且不要这样做这在你的 UI 线程上)。

The underlying Win32 process handle is waitable, and will be signalled when the process exits.

In native code:

DWORD res = WaitForSIngleObject(hProcess, INFINITE);
if (res == WAIT_OBJECT_0) {
  // process has exited.
}

In native code, you'll need to either create a custom subtype of WaitHandle for process handles, or use P/Invoke. The disadvantage of P/Invoke is that it is harder to combine (using WaitForMultipleObjects multiple waits, so you are not dedicating a thread just to wait on one thing).


Thanks to 0xA3: Just use Process.WaitForExit (there is an overload with a timeout to avoid indefinite waits, and don't do this on your UI thread).

日记撕了你也走了 2024-09-18 22:03:25

当父进程启动子进程时,通过命令行参数将父进程 ID 传递给子进程。

然后在子进程上,使用 Process.GetProcessById(int) 获取父进程并使用 Process.WaitForExit() 。或者,您可以使用 Process.Exited 事件在父进程退出时获取通知(请记住将 Process.EnableRaisingEvents 设置为 true,否则该事件将不会被引发)。

When the parent process starts the child process, pass the parents process ID to the child via command line arguments.

Then on the child process, use the Process.GetProcessById(int) to get the parent process and use Process.WaitForExit(). Alternatively, you use the Process.Exited event to get a notification when the parent process exits (remember to set Process.EnableRaisingEvents to true, otherwise the event will not be raised).

小兔几 2024-09-18 22:03:25

我制作了一个子进程管理库,其中通过双向 WCF 管道监视父进程和子进程。如果子进程终止或父进程终止,则会通知对方。
还有一个可用的调试器帮助程序,它会自动将 VS 调试器附加到已启动的子进程。

双向 WCF 通道是可扩展的,您可以处理进程启动和进程终止事件。

项目站点:

http://www.crawler-lib.net/child-processes

NuGet包:

https://www.nuget.org/packages/ChildProcesses
https://www.nuget.org/packages/ChildProcesses.VisualStudioDebug/

I've made a child process management library where the parent process and the child process are monitored due a bidirectional WCF pipe. If either the child process terminates or the parent process terminates each other is notified.
There is also a debugger helper available which automatically attaches the VS debugger to the started child process.

The bidirectional WCF channel is extendable and you can handle process start and process terminate events.

Project site:

http://www.crawler-lib.net/child-processes

NuGet Packages:

https://www.nuget.org/packages/ChildProcesses
https://www.nuget.org/packages/ChildProcesses.VisualStudioDebug/

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