Right now I am using Process.Kill() to kill a process. Is there a way though, instead of just killing it immediately, that I can like send a message to the process instructing it to close so that it can gracefully clean up and shut down. Basically, I'm looking for the equivlent to just clicking the red X in the upper right hand corner, which I believe DOES send a message to the application requesting a shut down.
"Gracefully" killing a process
This has been asked, and an answer has been accepted here: #2056253 –
Nador
@Nador Stratton Good find and yes the idea is the same, but this question is specifically asking about managed code (see tags [c#] and [.net]), which there is not yet an answer for on the other question –
Horologium
@Nador Stratton - This question is about graceful shutdown using managed code with C#, the link you put up is about win32 and the answers require pInvokes. Not the same at all. –
Whist
@Nador Stratton Ic is right. I was looking for a managed C# way to do this. He answered my question perfectly. –
Selfabasement
Ah, sorry.. My thought process was... This will work from .Net code using PInvoke.., You can call any Win32 API function from .Net using PInvoke. However, I understand if you're looking for "Pure" managed code. –
Nador
If the process has a windows interface (as you refer to the red "X"), you can try Process.CloseMainWindow()
. If it fails, you can fallback to Process.Kill()
.
nice and simple. –
Patristic
Is it possible to run something like this from shell? I have a very long process that i may need to shut down some of the way through. –
Disenthrone
What about background services or processes in the system tray that don't have a main window? There's really no graceful way to shut those down? –
Burnt
@Burnt I'm afraid that's right. The docs for CloseMainWindow even say "Kill is the only way to terminate processes that do not have graphical interfaces." If it's in the system tray, that might count as a window and you might be able to send a message to it. I haven't tried though. If it's a "right-click, close" kind of thing you might also be able to use Spy++ to see what messages are generated when you manually close them gracefully and try sending the same messages to the process to mimic manually closing it -- this would be more like a macro than anything else though. –
Horologium
Killing can not be graceful, perhaps you can signal the process to commit suicide. For signaling you have many options.
- SendMessage
- NamedPipes
- Named Mutex
- Sockets
It would depend on the process you're killing. As stated at on the relevant page for Process.Kill
, "Kill is the only way to terminate processes that do not have graphical interfaces." If there is a graphical window, then go with the answer by lc above; Process.CloseMainWindow
functions as the red X you referred to.
This seems rather hard to believe. So if I stop a background service in Task Manager, or by right clicking a tray icon and clicking "Close", Windows just kills the process instantly because it has no main window? That certainly hasn't been my experience. –
Burnt
@Burnt It has main window, it's just hidden –
Bandy
© 2022 - 2024 — McMap. All rights reserved.