|
-
July 5th, 2006, 10:04 AM
#1
kill a process cleanly
Hi I have a console program A that launches another application B I created. Application B uses a lot of threads that uses while loops to wait for certain events or to know when to exit when a certain variable is set.
I would like program A to be able to kill application B but I am afraid if I use TerminateProcess some of the threads might not exit cleanly. Meaning the "return 0" line will not be reached. Can anybody confirm or deny that.
On a second note, usually in program B I request the user to press a key on the keyboard which causes all the threads to terminate. Is there a way for program A to send a virtual keystroke to application B. Thanks a lot for any answer.
Amish
-
July 5th, 2006, 10:41 AM
#2
Re: kill a process cleanly
 Originally Posted by axr0284
On a second note, usually in program B I request the user to press a key on the keyboard which causes all the threads to terminate. Is there a way for program A to send a virtual keystroke to application B. Thanks a lot for any answer.
Yes, SendMessage with a WM_KEYDOWN or similar message.
A question : Can you change the code of program B, or is it a third-party program?
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
July 5th, 2006, 11:29 AM
#3
Re: kill a process cleanly
Does a console application have a message pump? I think not, so sending it a WM_KEYDOWN wouldn't work.
Better would be to write your applications so they did have a message pump if you want them to be shutdown that way.
There is a way to put a Windows message pump into a console app on windows - I have done it myself a long time ago with Win32 SDK. It's simply one of the WaitFor commands, possibly WaitForMultipleObjects.
There are other options for non-Windows, eg listening on a pipe. Although pipes are also operating-system specific (there is no standard IPC), it is a mechanism that is supported on many operating systems so you could write code in a portable way with just a few implementation specifics if you wanted to use pipes.
-
July 5th, 2006, 11:54 AM
#4
Re: kill a process cleanly
Correct.
another solution kernel objects
Kuphryn
-
July 5th, 2006, 11:57 AM
#5
Re: kill a process cleanly
 Originally Posted by NMTop40
Does a console application have a message pump? I think not, so sending it a WM_KEYDOWN wouldn't work.
Application A is a console application, but application B might be a GUI application.
 Originally Posted by axr0284
Application B uses a lot of threads that uses while loops to wait for certain events or to know when to exit when a certain variable is set.
I hope that these loops contain a blocking operation such as GetMessage or WaitForMultipleObjects or WaitForSingleObject.
 Originally Posted by axr0284
I would like program A to be able to kill application B but I am afraid if I use TerminateProcess some of the threads might not exit cleanly.
Yeah, it is not clean at all.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
July 5th, 2006, 02:08 PM
#6
Re: kill a process cleanly
I cannot be sure application B will be a gui or not. Yes it would be a third party program. That's the main problem Therefore the process A that will kill application B has to be able to emulate sending a keyboard command so that B thinks it came from the keyboard
Last edited by axr0284; July 5th, 2006 at 02:11 PM.
-
July 5th, 2006, 09:24 PM
#7
Re: kill a process cleanly
What you're talking about here is a kind of Inter-Process communication.
Why don't you try to use socket or pipe to make each process able to communicate (such as an instruction to exit and so on)?
-
July 6th, 2006, 09:15 AM
#8
Re: kill a process cleanly
Obviously I created application B for testing purposes but the program will have to work with other peoples's code also so mitsukai use your head before insulting people. I don't know how to use pipe under windows. I'll look into it. Thanks
Amish
-
July 6th, 2006, 10:11 AM
#9
Re: kill a process cleanly
 Originally Posted by [email protected]
What you're talking about here is a kind of Inter-Process communication.
Why don't you try to use socket or pipe to make each process able to communicate (such as an instruction to exit and so on)?
this wont work since he has no controll over application B, he needs to send a quit message to the proccess
-
July 6th, 2006, 11:04 AM
#10
Re: kill a process cleanly
So, the problem is : Exiting a child process without knowing anything specific about it?
I'm pretty sure you can find many answers, seeking on this forum.
Unfortunately, there is no universal, perfectly clean mean.
You can, for instance, enumerate all windows of the child process, and close (with WM_CLOSE messages) all top-level *visible* *enabled* windows, and as soon as they are closed, new ones might get enabled (for instance, if they were modal childrens).... Close them all.
If, for a reason, one of these window can't close after a delay... Say, 5 seconds, try to send WM_QUIT to the thread owning the window.
Once you have closed all the enabled visible windows, wait on the hProcess for a few seconds.
If the process is still alive you can try to close the disabled visible windows... You should not close hidden windows at all.
Then, once you can't close more visible windows, if the process is still alive after a few seconds, try to send WM_QUIT messages to all the GUI threads of the process.
If, after a few seconds, the process is still alive... Use TerminateProcess.
optionally:
If TerminateProcess doesn't work, or the thread which calls TerminateProcess blocks for a too long time, another thread of your A application should shutdown the computer with ExitWindowsEx.
If, after thirty seconds, it doesn't work, output a MessageBox saying:
"Please, shutdown manually the computer, with the Power button.
If it doesn't work, use the Reset button, and then, the Power button.
If it doesn't work, disconnect your computer from the sector.
If it is a laptop, or it doesn't work, shutdown the power of the whole building.
If it doesn't work, blow up your computer with a hammer.
If it doesn't work, try again with a rocket launcher.
If it doesn't work, try with an atomic bomb.
If it doesn't work, shoot yourself in the head."
If the MessageBox call fails, output the message with TextOut(GetDC(NULL),
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|