Keep Console.ReadLine always on last line

后端 未结 3 1613
别跟我提以往
别跟我提以往 2021-01-23 03:44

I have an application written in C# which takes continuously commands from the user via a while(true) loop and Console.ReadLine.

I also do have various background operat

3条回答
  •  不知归路
    2021-01-23 04:19

    This is very, very difficult to do. You end up writing your own console input and output methods that every thread has to call. If any thread doesn't cooperate, it just doesn't work right. But it's possible using the Console API.

    The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.

    You might be able to get away with using Console.ReadLine on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.

    You'll want to become very familiar with Windows consoles.

    Be forewarned: it's a lot of work. And probably not worth the effort.

提交回复
热议问题