Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and why does QuickEdit mode in Command Prompt freeze applications?

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:

  • Command Line Windows Hanging in RDP Windows
  • Windows Console Application Getting Stuck
  • How to disable QuickEdit Mode for individual scripts

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

like image 948
Whymarrh Avatar asked May 24 '15 00:05

Whymarrh


People also ask

What is QuickEdit mode?

Quick Edit mode is a Microsoft Windows feature which allows the user to select text (click and drag the mouse across the desired text) using the mouse in the Command Prompt window.

How do I turn off Quick Edit mode?

Choose Properties - uncheck QuickEdit Mode, and click OK. If you want to disable it for all future command prompts too, do the same with Defaults instead of Properties.

How do you insert a file in CMD?

If you want to set the default for inserting text at the prompt rather than typing over the text that is already there, select the Insert Mode option. To temporarily use the overtype mode from the command prompt, press the Insert key once. Press Insert again to revert to insert mode.


1 Answers

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.

like image 71
Hans Passant Avatar answered Sep 22 '22 18:09

Hans Passant