Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gracefully terminate a process?

Tags:

process

winapi

I want to terminate a number of processes, but I want to give each process the chance to save its data, ask the user about saving a file and even ignore the close request.

So TerminateProcess is out of the question, because it kills the process instantly. Another way would be to use SendMessage/PostMessage to send a WM_CLOSE to the main window, unfortunately I don't know anything about the windows of the processes, I only have the process id, so FindWindow doesn't help either. Is there any other way to find the main windows of a process?

In other words: Is there any way to terminate any process gracefully just like the Windows 7 task manager did when you clicked on "End Task"? (and not "End Process")

like image 450
Daniel Rikowski Avatar asked Jan 13 '10 09:01

Daniel Rikowski


People also ask

How do you stop a process gracefully?

By default, all the process killing commands use “SIGTERM”, which allows the program to run some code before it exits, thus allowing it to terminate “gracefully”. If you want to terminate the process forcibly, you can use “SIGKILL” instead.

Which signal gracefully terminate the process?

The right signal for termination is SIGTERM and if SIGTERM doesn't terminate the process instantly, as you might prefer, it's because the application has chosen to handle the signal.


2 Answers

EnumWindows enumerates all the top level windows in a process. GetWindowThreadProcessId gets the process and Id of each thread.

You now have enough information to gracefully close any GUI application.

You can send WM_CLOSE messages to any window you wish to close. Many windows handle WM_CLOSE to prompt the user to save documents.You can send a WM_QUIT message using PostThreadMessage to the discovered threads to cause the message loop to terminate.

User code is not allowed to call DestroyWindow from a different app or thread to the windows... if the app does not respond to WM_CLOSE or WM_QUIT requests you're back in TerminateProcess land.

This will not close console applications as the application process, and process that owns the window, are different.


Refer to T.s. Arun's answer below for the correct method for dealing with console applications.

like image 126
Chris Becke Avatar answered Sep 21 '22 08:09

Chris Becke


I'm not too sure about the win32 apis but you could shell execute the taskkill command line function.

taskkill /? taskkill /pid 1230 taskkill /im notepad.exe 

The /f switch would force the kill but not using it just sends the termination signal so the application closes gracefully.

like image 41
Andy E Avatar answered Sep 21 '22 08:09

Andy E