Reputation: 5695
I've got the following code:
bool loop = true;
LongbowWorkerThread Worker = new LongbowWorkerThread();
Thread w = new Thread(Worker.UpdateChannel);
w.Start();
string inText = string.Empty;
while (loop) {
inText = Console.ReadLine();
Console.WriteLine(inText);
}
At this point, the worker thread simply echoes a string every 5000ms. The problem is that the printed string overlaps the user's already-entered text, which isn't ideal and looks quite ugly (although it doesn't affect the actual data returned by Console.ReadLine.)
Is there a way to prevent this overlap?
Upvotes: 3
Views: 6207
Reputation: 74
You can use NullLib.ConsoleEx
to solve this problem.
Install it through nuget package manager
It re-implements the console input logic based on ReadKey and does not destroy the content already entered by the user when using WriteLine. Replace 'Console.WriteLine' and 'Console.ReadLine' in your program with 'ConsoleSc.WriteLine' and 'ConsoleSc.ReadLine' respectively.
Upvotes: 0
Reputation: 4297
You could use the Console.CursorLeft and Console.CursorRight properties to note the cursors location, shift it, output your text, then shift it back.
Edit: Here's a sample I just threw together, works well based on the 60 seconds of testing I threw at it.
int fooCursorTop = Console.CursorTop + 1;
var timer2 = new System.Timers.Timer(1000);
timer2.Elapsed += new System.Timers.ElapsedEventHandler(delegate
{
int tempCursorLeft = Console.CursorLeft;
int tempCursorTop = Console.CursorTop;
Console.CursorLeft = 0;
Console.CursorTop = fooCursorTop;
Console.WriteLine("Foo");
Console.CursorLeft = tempCursorLeft;
Console.CursorTop = tempCursorTop;
fooCursorTop++;
});
timer2.Start();
string input = string.Empty;
while (input != "quit")
{
input = Console.ReadLine();
Console.CursorTop = fooCursorTop;
Console.WriteLine(input);
fooCursorTop += 2;
}
Upvotes: 4
Reputation: 800
Did you try using Console.Out.Flush() after Console.WriteLine()? It should emit the data from buffer to output stream.
Upvotes: 0