C # console: write while reading and saving structure

I am writing a C # console application that connects to the server via TCP, uses a separate stream to listen for incoming messages from the server, and I want it to write them to the console while reading the message from the console. I was able to do this, but I have a problem.

The console displays this while I am typing and messages appear:

msg from server
msg from server
my msmsg from server
g per server

And I want it to separate my message from server messages like this:

msg from server
msg from server
msg from server
my msg to server

If I receive a message, I still want to keep my typed message, but I do not want it to mix with the received message, I want it to remain complete in the last line.

Can I do it? And How?

Can I move my message to the end of the console when a message arrives from the server?

+4
source share
4 answers

I believe that this is actually possible with the standard .NET Console API, using a bit of hacking. What you need to do is create an asynchronous Read / ReadLine method (which I assume you have already done, judging by your post) and enable it to be canceled as soon as new data appears. I am going to assume that you are sending input lines line by line. The pseudocode for the “new data handler” might be something like this:

  Save the current input buffer.
 Cancel the current asynchronous read.
 Reset the cursor position to the end of the last writing of new data.
 Output the new data.
 Save the cursor position.
 Restart the asynchronous read event.

In particular, you will need to mess with Console.CursorLeft and Console.CursorTop . You may be able to avoid some hackers by directly contacting the Win32 API console functions, but this may not be a concern.

Hope this helps.

+1
source

You cannot do this directly through BCL in C #. However, there are two main alternatives.

1) Rotate this into a graphics application and create separate I / O areas. This is the most “understandable” option from a custom POV, but it requires a GUI application instead of a console application.

2) P / Call to console API directly. This gives you full control over the console view in parts. See SetConsoleCursorPosition and WriteConsole, etc.

+1
source

You need to use lock on some object to stop both threads using the console at the same time. Declare something like this:

 public static class ThreadSafeConsole { private static object _lockObject = new object(); public static void WriteLine(string str) { lock (_lockObject) { Console.WriteLine(str); } } } 

Now call ThreadSafeConsole.WriteLine instead of Console.WriteLine .

+1
source

If you understand correctly, you will need to determine what the beginning and end of “input to the console” is for your application so that you can still block the object while it is running, rather than inserting messages from your listener stream into your text.

 public static Object consoleLock = new Object(); public static void Main(string[] args) { lock (consoleLock) { // now nothing can write to the console (if it trying to lock onto it) Console.WriteLine("Please Input Something"); // read from console } // now, your separate thread CAN write to the console - without // interrupting your input process } 
0
source

All Articles