在 Windows 上运行任意子进程并仍然干净地终止? [英] Run arbitrary subprocesses on Windows and still terminate cleanly?

查看:30
本文介绍了在 Windows 上运行任意子进程并仍然干净地终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序 A,我希望能够调用用户在配置文件中指定的任意其他进程.

批处理脚本 B 是一个用户希望 A 调用的进程.B 设置一些环境变量,显示一些消息并调用编译器 C 来完成一些工作.

Windows 是否为干净地终止任意进程提供了标准方法?假设 A 在控制台中运行并收到 CTRL+C.它可以将其传递给 B 和 C 吗?假设A在一个窗口中运行,用户试图关闭该窗口,是否可以取消B和C?

TerminateProcess 是一种选择,但不是一个很好的选择.如果 A 在 B 上使用 TerminateProcess,则 C 继续运行.如果 C 长期运行,这可能会导致严重的问题,因为我们可能会启动另一个 C 实例来操作相同的文件,而第一个 C 实例仍在秘密工作.此外,TerminateProcess 不会导致干净退出.

GenerateConsoleCtrlEvent 听起来不错,当一切都在控制台中运行时可能会起作用,但文档说您只能将 CTRL+C 发送到您自己的控制台,因此如果 A 在窗口中运行,则无济于事.

在 Windows 上有什么等同于 SIGINT 的吗?我很想找到一篇这样的文章:http://www.cons.org/cracauer/用于 Windows 的 signint.html.

解决方案

我想我在这个问题上有点晚了,但无论如何我都会为有同样问题的人写一些东西.

我的问题与我的问题类似,因为我希望我的应用程序是一个 GUI 应用程序,但执行的进程应该在后台运行,而不附加任何交互式控制台窗口.

我设法使用 GenerateConsoleCtrlEvent() 解决了这个问题.棘手的部分只是文档并没有真正清楚地说明如何使用它以及它的陷阱.

我的解决方案基于此处.但这也并没有真正解释所有细节,所以这里是有关如何使其工作的详细信息.

  1. 创建一个新的帮助应用程序Helper.exe".此应用程序将位于您的应用程序(父)和您希望能够关闭的子进程之间.它还将创建实际的子进程.您必须有这个中间人"进程,否则 GenerateConsoleCtrlEvent() 将失败.

  2. 使用某种 IPC 机制从父进程向助手进程传达助手应该关闭子进程的信息.当助手获得此事件时,它会调用GenerateConsoleCtrlEvent(CTRL_BREAK, 0)",从而关闭自身和子进程.我自己为此使用了一个事件对象,当父进程想要​​取消子进程时,它会完成该对象.

要创建 Helper.exe,请使用 CREATE_NO_WINDOW 和 CREATE_NEW_PROCESS_GROUP 创建它.并且在创建子进程时创建它没有标志(0),这意味着它将从其父进程派生控制台.不这样做将导致它忽略该事件.

像这样完成每一步是非常重要的.我一直在尝试各种不同的组合,但这种组合是唯一有效的组合.您不能发送 CTRL_C 事件.它会返回成功,但会被进程忽略.CTRL_BREAK 是唯一有效的方法.并不重要,因为它们最终都会调用 ExitProcess().

您也不能使用子进程 ID 的进程分组 ID 直接调用 GenerateConsoleCtrlEvent(),从而允许辅助进程继续生存.这也会失败.

我花了一整天的时间试图让它发挥作用.此解决方案对我有用,但如果有人要添加任何其他内容,请执行.我去了所有其他网络,发现很多人有类似的问题,但没有明确的问题解决方案.GenerateConsoleCtrlEvent() 的工作原理也有点奇怪,所以如果有人知道更多细节,请分享.

I have an application A that I would like to be able to invoke arbitrary other processes as specified by a user in a configuration file.

Batch script B is one such process a user would like to be invoked by A. B sets up some environment variables, shows some messages and invokes a compiler C to do some work.

Does Windows provide a standard way for arbitrary processes to be terminated cleanly? Suppose A is run in a console and receives a CTRL+C. Can it pass this on to B and C? Suppose A runs in a window and the user tries to close the window, can it cancel B and C?

TerminateProcess is an option, but not a very good one. If A uses TerminateProcess on B, C keeps running. This could cause nasty problems if C is long-running, since we might start another instance of C to operate on the same files while the first instance of C is still secretly at work. In addition, TerminateProcess doesn't result in a clean exit.

GenerateConsoleCtrlEvent sounds nice, and might work when everything's running in a console, but the documentation says that you can only send CTRL+C to your own console, and so wouldn't help if A were running in a window.

Is there any equivalent to SIGINT on Windows? I would love to find an article like this one: http://www.cons.org/cracauer/sigint.html for Windows.

解决方案

I guess I'm a bit late on this question but I'll write something anyway for anyone having the same problem.

My problem is similar in that I'd like my application to be a GUI application but the processes executed should be run in the background without any interactive console window attached.

I managed to solve this using GenerateConsoleCtrlEvent(). The tricky part is just that the documentation is not really clear on exactly how it can be used and the pitfalls with it.

My solution is based on what is described here. But that didn't really explain all the details either, so here is the details on how to get it working.

  1. Create a new helper application "Helper.exe". This application will sit between your application (parent) and the child process you want to be able to close. It will also create the actual child process. You must have this "middle man" process or GenerateConsoleCtrlEvent() will fail.

  2. Use some kind of IPC mechanism to communicate from the parent to the helper process that the helper should close the child process. When the helper get this event it calls "GenerateConsoleCtrlEvent(CTRL_BREAK, 0)" which closes down itself and the child process. I used an event object for this myself which the parent completes when it wants to cancel the child process.

To create your Helper.exe create it with CREATE_NO_WINDOW and CREATE_NEW_PROCESS_GROUP. And when creating the child process create it with no flags (0) meaning it will derive the console from its parent. Failing to do this will cause it to ignore the event.

It is very important that each step is done like this. I've been trying all different kinds of combinations but this combination is the only one that works. You can't send a CTRL_C event. It will return success but will be ignored by the process. CTRL_BREAK is the only one that works. Doesn't really matter since they will both call ExitProcess() in the end.

You also can't call GenerateConsoleCtrlEvent() with a process groupd id of the child process id directly allowing the helper process to continue living. This will fail as well.

I spent a whole day trying to get this working. This solution works for me but if anyone has anything else to add please do. I went all other the net finding lots of people with similar problems but no definite solution to the problem. How GenerateConsoleCtrlEvent() works is also a bit weird so if anyone knows more details on it please share.

这篇关于在 Windows 上运行任意子进程并仍然干净地终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆