C Smith C Smith - 1 month ago 8
C# Question

C# allow simultaneous console input and output

I'm trying to learn my way around C# threading by making a simple little game. I've run into a bit of an issue that I could certainly use help with.

My desire is to have simultaneous input and output. The output from the threads would appear at the top of the screen, while the user's input could be typed to the bottom of the screen. The problem I am running into is that in order to refresh the screen, when I use

, it also wipes out whatever the user is typing! Below I have attached an extremely simplified version of what I am trying to do (in order to avoid unnecessary code getting in the way of the actual issue).

Please note: While in this example I am only updating a single character at the top of the screen, my actual program would have LOTS of text all over the top and middle of the screen that would be constantly changing with every tick (which I plan to use at 1.5 seconds).

Any ideas would be great! I'm still new to C# programming and would be thrilled for any help you could give :) The only thing here that I am attached to is the design. System output at the top, user input at the very bottom preceeded by ">". If the way I am doing it is wrong, I have no issue with tossing it all out the window and doing it a different way.

EDIT: My goal here is to have the output text at the top of the screen update every 1.5 seconds (every run of the count thread) while allowing the user to type at the bottom of the screen. However, the method I am using to do that (clearing and then writing new content to the screen) is also wiping out the user's input! It makes it so that every 1.5 seconds whatever input the user is typing just disappears, and rightfully so since that's exactly what Console.Clear does. So I'm looking for a new method to accomplish this task.

In short: How do I update text at the top/middle of the screen while allowing the user to continue typing at the bottom of the screen?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
class Program

static int i = 0;

static void Main(string[] args)
Thread tickThread = new Thread(new ThreadStart(CountThread));
Thread userThread = new Thread(new ThreadStart(UserInput));

static void UserInput()
string input = "";
while (true)
input = Console.ReadLine();

static void CountThread()
while (true)
Console.SetCursorPosition(0, 0);
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write("> ");



Answer Source

You can set cursor position in a loop for each cell in the console except those, which are designed for user's input, and write a space symbol. This will, essentially, clear a part of a console window. Also, you can use the same method to partially render output to the console.