如何捕获控制台关闭事件?
我的问题的背景是:
- 我有一个 Windows .NET 应用程序(GUI)作为主进程运行。
- 从这个(父)进程中,我创建了几个子进程作为控制台进程。
- 主进程通过命名管道向子进程发送数据。
- 在主应用程序中,我有一个子流程列表。
我的问题是每个控制台都有一个关闭(“x”)按钮并且可以终止(无论它是什么方式)。由于我在主应用程序中保留了创建的控制台的列表,因此我想知道控制台何时被终止或退出。
我的控制台(子进程)程序只是一个“main()”,带有一个读取管道(并显示数据)的循环函数。它没有消息系统或任何其他可以处理窗口“退出”的系统。
我想到的第一个想法是从主应用程序轮询子进程以刷新列表。但这意味着我必须引入一个计时器或一个监视控制台的线程。我不喜欢这个主意。
有人有更好的主意吗?
The context of my problem is:
- I have a Windows .NET app (GUI) running as a main process.
- From this (parent) process, I create a couple of sub-processes as console processes.
- The main process sends data to the children processes through named pipes.
- In the main app, I have a list of the sub-processes.
My probleme is that each console has a close ("x") button and can be terminated (whatever the way it is). Since I keep a list of the created consoles in my main app, I would like to know when a console is killed or exited.
My console (child process) program is simply a "main()" with a loop function that reads the pipe (and displays the data). It has no message system or whatever else that could handle a windowing "exit".
The first idea that comes to my head is to poll the sub-processes from the main app to refresh the list. But this means I have to introduce a timer or a thread that watches the consoles. I don't like the idea.
Does someone have a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WaitForSingleObject(hThread, 0)
将告诉您hThread
参数中指定的线程是否已发出信号并因此完成。hProcess
也是如此。子进程的两个句柄都会在调用
CreateProcess()
后返回。您可以立即关闭它们,也可以使用WaitForSingleObject
进行监视。WaitForSingleObject(hThread, 0)
will tell you whether the thread specified inhThread
argument is signaled and therefore finished. Same goes tohProcess
.Both handles of your child process are returned after
CreateProcess()
call. You can either close them immediately, or monitor usingWaitForSingleObject
.