Take the 2-minute tour ×

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.

share|improve this question
This has been asked, and an answer has been accepted here: stackoverflow.com/questions/2055753/… –  David Stratton Aug 5 '10 at 5:33
@David 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 –  lc. Aug 5 '10 at 5:35
@David 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. –  Oded Aug 5 '10 at 5:37
@David Stratton Ic is right. I was looking for a managed C# way to do this. He answered my question perfectly. –  icemanind Aug 5 '10 at 5:37
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. –  David Stratton Aug 5 '10 at 5:39

3 Answers 3

up vote 17 down vote accepted

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().

share|improve this answer
This worked Perfect! Thanks for the quick and simple answer! –  icemanind Aug 5 '10 at 5:37

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.

share|improve this answer

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
share|improve this answer

Your Answer


