Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cleanly shut down a console app started with Process.Start?
    primarykey
    data
    text
    <p>This is looking like an impossible task. Absolutely nothing I've found works. The question is how to cleanly close a console application started with Process.Start that has been started with no console window and without using shell execute: (<code>ProcessStartInfo.CreateNoWindow = true; ProcessStartInfo.UseShellExecute = false;</code>). </p> <p>It is given that the application being started will shut down "cleanly" if it receives a ctrl-c or ctrl-break signal, but there seems to be no way to send it one that works (particularly GenerateConsoleCtrlEvent).</p> <ul> <li>Process.Kill doesn't work. It leaves corrupt files behind due to abrupt killing of the process.</li> <li>Process.CloseMainWindow doesn't work. There is no main window in this case, so the function returns false and does nothing.</li> <li>Calling EnumThreadWindows on all threads for the process and sending a WM_CLOSE to every window does nothing, and there aren't any thread windows anyway.</li> <li>GenerateConsoleCtrlEvent doesn't work. It's only useful for processes in the same group (which .NET gives you no control over), with an unwanted side effect of closing the calling process anyway. The function does not allow you to specify a process id.</li> </ul> <p>Whoever can provide code that accepts a "Process" object started with the parameters above which results in a clean shutdown of the started process without affecting the calling process will be marked as the answer. Use 7z.exe (7-zip archiver) as an example console app, which begins compressing a large file, and will leave a corrupt, unfinished file behind if not terminated cleanly.</p> <p>Until someone provides a functional example or code that leads to a functional example, this question is unanswered. I have seen dozens of people asking this question and dozens of answers online, and none of them work. .NET seems to provide no support for cleanly closing a console application given its process id, which is odd considering it's started with a .NET Process object. Part of the problem is the inability to create a process in a new process group, which makes using GenerateConsoleCtrlEvent useless. There has to be a solution to this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COSounds to me like the correct solution is `GenerateConsoleCtrlEvent`. So you just need to figure out how to create a process in a new group. If that's not possible using the .NET process wrapper, drop down to the native Win32 function. No harm in using P/Invoke, you're already doing it for `GenerateConsoleCtrlEvent` anyway.
      singulars
    2. COI'm not already using GenerateConsoleCtrlEvent (it doesn't work), and I'd rather not use P/Invoke. The SECURITY_ATTRIBUTES struct used by CreateProcess has a pointer that requires me to compile with `unsafe`, which I'm not doing. Another problem with CreateProcess is I can't use Process.EnableRaisingEvents and the Exited event reliably, because the process could finish before I get a chance to call Process.GetProcessById to create a .NET Process instance to assign the handler, which normally would be done before calling Process.Start. I'm being forced to re-write most of the logic in Win32.
      singulars
    3. COAnd it doesn't work anyway, as I said. Despite creating the process with CREATE_NEW_PROCESS_GROUP, GenerateConsoleCtrlEvent has no effect on the group, as identified by the process id. I tried passing the constant for CTRL+C, and then CTRL+BREAK, and neither work. Also, along with CREATE_NEW_PROCESS_GROUP, I tried both of the flags for either CREATE_NO_WINDOW or DETACHED_PROCESS (separately, since they are mutually exclusive), and while both achieve a console-less process that doesn't interfere with my main application's console window, the GenerateConsoleCrtlEvent method doesn't work.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload