Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

windows - 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:

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...