如何使用MFC从另一个进程中关闭一个进程 [英] How to close a process from another process using MFC

查看:258
本文介绍了如何使用MFC从另一个进程中关闭一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我从我的应用程序创建一个进程,现在我想在我的主应用程序关闭时关闭另一个进程.我不想杀死/终止另一个进程,我想关闭另一个进程,我特别要说关闭,因为我有返回另一个进程关闭的日志文件,因此,如果我终止该进程,则不会创建那些日志文件,这就是我要通过MFC应用程序关闭进程的原因.请为此提供任何指南. ="h2_lin">解决方案

那么什么是关闭"? 没有这样的东西.

当调用TerminateProcessKill时,或者过程本身结束时,过程终止.例如,当进程从其入口点函数(如main)返回时.

您不能强制某个进程执行此操作,除非该进程经过专门设计以根据来自其他进程的某些命令终止自身.您可以始终这样做,但是为此,您需要成为应用程序的作者,您需要停止并使用IPC( http ://en.wikipedia.org/wiki/进程间通信 [ ^ ]).如果是这种情况,并且您需要使用IPC的帮助,请提供一些反馈并进行更多说明-最有可能的,我将为您提供帮助.

您不能对任意过程执行此操作.您只能执行TerminateProcess.

但我不建议您这样做.从您的问题中,我知道您知道原因. :-)

—SA


您有2个进程:A和B.A由用户启动,B是A的子进程.通常有两种方式终止应用程序的执行:用户可以礼貌地要求应用程序退出并完成其活动(通过在控制台中键入一些用于该程序的命令,或者通过按下发送WM_CLOSE的Windows应用程序上的close X按钮)给编的消息).如果您的A进程收到这样的礼貌退出,那么有时建议您询问用户是否真的要退出(带有是/否消息框,例如警告未保存的文档).如果它决定您必须作为对退出请求的响应而退出,那么您必须尽快清理并退出.对于某种编排来说,这可能很快就会很长,几秒钟或几分钟,在这种情况下,您应该为用户提供一些反馈(类似进度条的内容).作为此清理过程的一部分,您必须将此清理请求从进程A转发到其子进程(B),并且必须等待子进程的正常退出.从父进程到子进程的通知可能是一个非常愚蠢的机制,例如在某个位置创建文件,然后子进程定期轮询该文件的存在,或者对于某些IPC(如回送套接字或管道)可能非常复杂.

终止进程的另一种方法是毫无疑问地终止它.如果用户杀死了您的A进程,则其子进程可能仍然存在.因此,人们通常在作业中启动根流程(例如您的流程A),结果,由流程A启动的所有直接/间接流程将自动进入其父流程的工作.这样,您可以杀死整个作业及其内部的流程.要了解如何在Windows上执行此操作,请搜索 CreateJobObject () [ ^ ]相关教程,在此不再赘述.请注意,即使是Visual Studio的调试器,也会在作业中启动已调试的进程,因此您的作业管理代码可能与此冲突(我曾经遇到过此问题).


Hello,

I creating a process from my application,now i want to close the other process when my main application is closed.I don''t want kill/Terminate the other process i want close the other process,i am specifically saying close because i have return some log file closing of the other process so if i terminate the process those log files wont be created thats the reason i want to close the process through my MFC application.Kindly can any one guide for this.Thankyou in Advance.

解决方案

What is "close" then? There is no such thing.

The process is terminated when TerminateProcess or Kill is called, or if the process end by itself; for example, when a process returns from its entry point function like main.

You cannot force a process to do it unless it is specially designed to terminate itself on some command which came from some other process. You can always do it, but for this purpose you need to be the author of the application you need to stop and use IPC (http://en.wikipedia.org/wiki/Inter-process_communication[^]). If this is the case and you need help using IPC, please provide some feedback and explain more — most likely, I would be able to help.

You cannot do it to an arbitrary process. You can only do TerminateProcess.

But I don''t recommend you to do so. From your question, I know that you know why. :-)

—SA


You have 2 processes: A and B. A is started by the user and B is the child process of A. Usually there are 2 ways of ending the execution of an application: The user can politely ask the application to exit and finish its activities before doing so (by typing in some commands for the program in the console, or by pressing the close X button on the windows applications that send WM_CLOSE message to the prog). If your A process receives such a polite exit then sometimes its recommended to ask the user if he really wants to exit (with a yes/no messagebox that warns for example unsaved documents). If its decided that you have to exit as a response to the exit request then you have to cleanup and exit as soon as possible. This as soon as possible can be quite long for some kind of progs, seconds or a few minutes, in this case you should give some feedback (progressbar-like something) for the user. As part of this cleanup process you have to forward this cleanup request from process A to its child process (B) and you have to wait for the graceful exit of the child process. This notification from the parent to child process can be a very stupid mechanism like creating a file somewhere and the child polls the existence of that regularly or it can be very sophisticated with some IPC like loopback socket or pipe.

The other way of terminating a process is killing it without questions. If the user kills your A process then its a problem that its child processes might stay alive. For this reason people usually start the root process (like your process A) in a job and as a result all direct/indirect processes launched by process A will automatically go into the job of their parent process. This way you can kill a whole job with the processes inside it. To find out how to do it on windows search for CreateJobObject()[^] related tutorials on the net, I wont describe it here. Note that even the debugger of Visual Studio starts your debugged processes in jobs so your job management code might have conflict with that (I faced this problem once).


这篇关于如何使用MFC从另一个进程中关闭一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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