我有打开子进程的父进程。仅当父进程不再运行时,我才需要执行某些功能。
知道父进程没有运行的最佳方法是什么?因为它可以被暴力终止,所以我不想创建一些在关闭事件时向我的子进程发送信号的功能。
或者只是像这样查找我的父进程:
在父进程中进行此操作并将其传递给子进程 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 ..
发布评论
评论(4)
下面是一个如何使用 Process.WaitForExit 来检查 id 已在命令行上传递的父进程的简单示例:
可以使用 Process.Exited 事件来完成像这样:
请注意,在两个示例中,
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 the
Process.Exited
event could be done like this: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.底层 Win32 进程句柄是可等待的,并且当进程退出时将收到信号。
在本机代码中:
在本机代码中,您需要创建
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:
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 (usingWaitForMultipleObjects
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).当父进程启动子进程时,通过命令行参数将父进程 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 useProcess.WaitForExit()
. Alternatively, you use theProcess.Exited
event to get a notification when the parent process exits (remember to setProcess.EnableRaisingEvents
totrue
, otherwise the event will not be raised).我制作了一个子进程管理库,其中通过双向 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/