[SOLVED]Multi-threading Problem with Console Input and Output
Hello everyone,
My question in a short (but maybe in a wrong access) form:
Is there any methode to get the characters of an UNFINISHED user input? "Unfinished" means the user has inputed something but has not pressed "Enter" key yet.
My question in a long and detailed form:
I want to implement a server program, that has threads for each connection. Each threads should report its state via a console output --- such as a new connection has been established; meanwhile the main thread should be able to control the connections via console input --- such as close a connection, or send some messages to an specified clients.
I tried console input and output in a "normal way", but if a "connection thread output" comes after the main thread shows the prompt of the control command, everything is messed up like this:
command> Connection reports goes here
reports goes here
and here
and here
_ <--- finally here is the cursor of new control command.
I've already tried "\b", before connection thread outputs any reports, output "\b"s back to the start of a line and then output the reports and finally append a "command> " after the reports. It also has problems. If the server user input something after the command prompt, but has not finished yet, while a new report from the connection thread comes. It looks like:
commConnection reports goes here
reports goes here
and here
and here
command> _
because I have outputed 9 "\b"s to overlap "command> "; however, if user has already typed something like "command> clos", then the last 9 chars will be overlaped, so "comm" remains.
Is there any way to solve the problem? Thanks a lot! And sorry for my English if I typed something wrong, because English is not my mother tongue.
racnil
Re: Multi-threading Problem with Console Input and Output
Do you have multiple threads writing to the console?
Re: Multi-threading Problem with Console Input and Output
Quote:
Originally Posted by
Tolls
Do you have multiple threads writing to the console?
Hello Tolls,
Thanks for your reply.
Yes, there are threads writing to the console, and one of them even reads from the console. But how can we synchronize among the threads?
Zihan
Re: Multi-threading Problem with Console Input and Output
I would recommend not doing that.
If you need to display stuff coming in from one thing, while listening for use rinput on another, then this should probably be a Swing/JavaFX GUI, so you can have different components.
Re: Multi-threading Problem with Console Input and Output
Thanks Tolls!
I am also considering to convert the UI to swing instead of console. It's practical.