3

Suppose I start a Process for running a console application like 7z.exe (7-Zip Archiver). I am creating the Process without a window and I'm redirecting the standard output.

I want to know how to stop the Process other than "Process.Kill", because that causes an incomplete/corrupt archive file to be left behind.

If I press CTRL-BREAK when running 7z.exe from a normal command console window, it shuts down gracefully and deletes the incomplete file.

If cmd.exe can start a process and send it a ctrl-break signal, then how can my own application start a process and send it a ctrl-break (or equivalent) signal, so that the process shuts down gracefully?

Triynko
  • 18,766
  • 21
  • 107
  • 173
  • 1
    http://stackoverflow.com/questions/1453520/run-arbitrary-subprocesses-on-windows-and-still-terminate-cleanly – idursun Apr 19 '13 at 20:15
  • I've read that before... that you need a middle-man process to get GenerateConsoleCtrlEvent to work, since it really only works on the calling process, despite appearing to accept an arbitrary process group id. The answer in that linked question explains it a little better, so I'll try that. – Triynko Apr 22 '13 at 19:46

1 Answers1

0

You can do a thread.sleep to pause it if need be. If you want to stop it completely I'd set up a watch variable then pause your thread, handle any existing data (this is probably was 7zip is doing), and then do a process.kill. I don't think there is (or know of one) an way to automagically handle aborting a process "gracefully". It is up to you, the programmer, to handle an interrupted exit condition gracefully.

Pseudonym
  • 2,052
  • 2
  • 17
  • 38
  • 2
    7-Zip responds to ctrl+break or ctrl+c to specifically shut down gracefully. That is a given. The issue at hand is how to send it that signal from the process that started it. The GenerateConsoleCtrlEvent is specifically meant for this purpose, but even when 7z.exe is run in a new process group, and regardless of whether it's created with a console window or not (either create_no_window, create_new_console, or detached_process process creation flags), it just doesn't work. The application never receives the signal and doesn't shut down as it normally would in response to such a signal. – Triynko Apr 22 '13 at 19:38