Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.
Posted
Comments
pasztorpisti 24-Sep-12 5:50am    
I think the 1 as a vote to this question is unfair because doing this whole thing well isn't that simple as many think.

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
 
Share this answer
 
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).
 
Share this answer
 
Comments
Rocky_Bas 24-Sep-12 6:07am    
Hello pasztorpisti thanks for the reply.The actual problem in my application is, iam having two different applications A and B,So in A is the main application, in this application i have created a process by calling the exe of the B application(Using this function CreateProcess) to genearate some log files.


As i have written some code in B app while closing and destroying of the exe to log some data.So if i terminate or kill the exe of B application then the application would be terminated and due to this close and destroy functions will not be called
and due to that i am not able to get the log file.Thats the reason iam asking you can i close a exe from my MFC application.
pasztorpisti 24-Sep-12 6:33am    
You should never use TerminateProcess(). Using it you can not guarantee anything. You should somehow send a polite exit request to your B process and you should wait for it to exit. In process B you must be able to receive the request or check it periodically (for example in every second or so) and if you detect an exit request then cleanup+exit.
pasztorpisti 24-Sep-12 6:13am    
x

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900