Multithreaded console I/O

前端 未结 3 886
旧巷少年郎
旧巷少年郎 2020-12-31 16:30

I\'m using a console in my multithreaded application. Right now, it only accepts output (printf and the like) and so far I have no issues. However, I want to be able to su

3条回答
  •  离开以前
    2020-12-31 17:04

    You really don't want to go down the road of trying to reserve part of the console for input while writing to the rest of the console. At least, not if you're just writing scrolling text. It's possible, but fraught with error and way more trouble than it's worth. See Async Console Output for a few hints of the problems.

    Certainly, it's not possible to do this using just conio.h.

    You could allocate two console screen buffers, with one being for input and one for program output. When your program is running normally, the output screen buffer is selected and you see the output scrolling on the screen. But when your program is waiting for user input, you swap screen buffers so that the output is still going, but in the other screen buffer.

    You end up having to format the output yourself and call WriteConsoleOutput, passing it the handle of the screen buffer you want to write to. It gets complicated in a real hurry, and it's very difficult to get right. If it's even possible. I know I've spent way too much time on it in the past, and there were always odd problems.

    I won't say that what you want to do isn't possible. I will say, however, that you're going to have a tough time with it.

提交回复
热议问题