Recently I have started work on a multi-threaded console based application using VB.Net and am having an (explainable) error. The program itself is an server that receives socket connections from outside apps in different threads. I also have a separate thread that simply waits for a user to enter text into the app using console.readline (so that server commands can be entered such as HELP or STATUS).
The problem is that the program outputs messages from the different socket threads, and if I am in the middle of typing a command, it gets broken up. If I enter the command “status”, it works as it should, but for the user entering the command, it is broken up and they may not be able to see if they are typing the command wrong. For example:
staNew client connected.
tus
I know why this is happening, but I am curious if there is a simple fix to stop this (without having to pause the other threads during input) or possibly an easier way to allow console commands to be entered that will not be moved around on additional app output.
I know that this issue is mostly aesthetic, but stuff like this annoys me. I hate to answer my own question, but I actually came up with a pretty decent solution for this based on some input from others. I will attach a code sample in case anyone wants to do something similar in the future. I know there are probably better ways to do this, but here’s what I did.
Basically instead of using console.writeline from each thread, I created a new sub routine (TextOut) which has some logic behind it. I also updated my input thread to read each char into a shared string instead of using console.readline(). Here is the code: