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.
+6
A:
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()
.
lc
2010-08-05 05:32:06
This worked Perfect! Thanks for the quick and simple answer!
icemanind
2010-08-05 05:37:03
Whats stopping you from accepting the answer if its perfect.
Hasan Khan
2010-08-05 05:40:21
It won't let me yet..Stack Overflow makes you wait like 10 minutes before accepting an answer.
icemanind
2010-08-05 05:44:49
I see. SO has starting restricting actions a bit too much I think. A website shouldn't 'dictate' how to use it.
Hasan Khan
2010-08-05 06:24:45
+1
A:
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.
JBirch
2010-08-05 05:41:21
+1
A:
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
Hasan Khan
2010-08-05 05:42:03