81

I recently ran into an issue with Command Prompt on Windows where QuickEdit mode was enabled and clicking the window was selecting text and hanging a running program. This is, apparently, known behaviour—I found a few questions related to it:

How is the application "paused"/"suspended"? Is the process similar to the SIGSTOP signal on *nix? (I am also interested in understanding why this functionality exists in the first place? It seems unintuitive and dangerous.)

Community
  • 1
  • 1
Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • 1
    Sorry, can't tell you How. For the "Why?": I can imagine, it's quite difficult to select some text while it's scrolling. – Stephan May 24 '15 at 07:25
  • 2
    This is answered reasonably well here: http://superuser.com/questions/459609/what-does-it-do-exactly-if-i-click-in-the-window-of-cmd – Scott C May 25 '15 at 10:40
  • @ScottC good find, thanks! However, I am really trying to understand the how under the hood (and I would love to know the *why*). Also, some comparison to *nix. – Whymarrh May 27 '15 at 10:59
  • Here's a Python solution: https://stackoverflow.com/a/76855923/15016163 – Maicon Mauricio Aug 09 '23 at 01:12

2 Answers2

88

This is very much by design. There's no reasonable way a user can select text when your program keeps scrolling the content of the console window. So the console host program simply stops reading your stdout/stderr output and your program hangs until the user completes the operation. This can be changed, you'll have to call Get+SetConsoleMode() and turn off the ENABLE_QUICK_EDIT_MODE option.

Do note that this "hang" isn't fundamentally different from the execution pauses you get when your program generates stdout output at a rate far higher than the console host can consume it. Albeit that those delays are finite.

And it isn't the only way the user can stop your program, they can also simply press Ctrl+S. Pressing Ctrl+Q resumes it again. If you're old enough then you might recognize these control codes as Xon/Xoff, handshake characters for a terminal. Which is what a console really is, a simple emulation of a terminal the way they were used back in the 1970s. This can be changed too, you'll have to stop relying on the built-in buffered console input and switch to ReadConsole(). Or by turning off the ENABLE_LINE_INPUT console option, not so sure what side-effects that has since you didn't mention any language runtime, you'll have to try.

And of course terminating your program is very easy. You get EOF on stdin when the user types Ctrl+Z, that ought to end your program. And there is Ctrl+C and Ctrl+Break for an instant termination, regardless what your program is doing. You can get a notification for these with SetConsoleCtrlHandler() but you can't block it.

If the default behavior is dangerous and risk the health of a human then I'd strongly suggest you hire a consultant. And won't know who wrote this answer.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2
    Thanks! I certainly meant "dangerous" in the sense that it is likely to cause problems. You compare the pause to the behaviour a program exhibits when generating output too fast, does that mean that execution pauses on the `Console.WriteLine` until the selection is made? – Whymarrh May 29 '15 at 21:28
  • 4
    It must be somehow connected with Windows design and some compatibility concerns, but still on any Linux you can just select text from terminal and the program does not stop. – Rafał Kłys Mar 10 '16 at 06:16
  • Probably useful info: Redirecting the output and/or error streams will prevent quick edit mode from pausing your program. This might be helpful in some cases, too, if you do not want to change any settings of your console and are fine capturing your output somewhere else... – stackprotector May 01 '20 at 07:11
8

To answer the how I was recently debugging the same problem in a python script and captured a stack trace using windbg.

The call to WriteConsole eventually leads to the NtDeviceIoControlFile syscall and the kernel does not return until a keypress happens or the QuickEdit mode is changed. So if you never write to the console, your process is safe from QuickEdit mode freeze. And your user will never need to copy anything. So wait what is QuickEdit mode for again?

SargeATM
  • 2,483
  • 14
  • 24