C
C#•6mo ago
Bandana Dee(z)

is there a way to do Console.ReadLine() asynchronously?

what the title says. it's always a blocking method, no matter which way you spin it, so im not sure if there's like a workaround or something
20 Replies
cap5lut
cap5lut•6mo ago
directly on Console are helper methods for the different streams, Console.In, Console.Out and Console.Error provide a richer API. Console.In.ReadLineAsync()
Bandana Dee(z)
Bandana Dee(z)•6mo ago
yeah but that method is explicitly stated to be broken:
Bandana Dee(z)
Bandana Dee(z)•6mo ago
No description
cap5lut
cap5lut•6mo ago
oh then there probably isnt a way
Bandana Dee(z)
Bandana Dee(z)•6mo ago
🔫 found a hacky af way i put it in a async method and.. dont await the async method this will probably cause some drastic error but uh
Buddy
Buddy•6mo ago
Why do you even need an asynchronous API?
Bandana Dee(z)
Bandana Dee(z)•6mo ago
making a simple chatbox app because im testing lite net lib (im probably doing this wrong) and if ReadLine() is blocking then you cant really recieve any messages
Bailey
Bailey•6mo ago
Hello, I 'm just thinkng of what you want to do. Even if you can wait for a inputline, this wouldnt work. The problem is that the main text is printed in the console and also waiting. So you cant block without having a chat window different from a input window. However if you only want the program process to continue you could do the following: - create an object async and start it with e.g. task / background worker (completely loose from the main program). - in the different thread add an event to communicate with the main program. In short the console runs in another thread and the main program is informed for any input using an event subscription
cap5lut
cap5lut•6mo ago
i would do it vice versa. do the blocking console stuff in the main thread and everything else which can work async throw onto the thread pool often the main thread is the only foreground thread, thus it needs to stay active because thread pool threads are background threads eg this little main method
public static async Task Main()
{
await Task.Delay(1000);
}
public static async Task Main()
{
await Task.Delay(1000);
}
becomes something like
public static async Task NotTheMainMethod()
{
await Task.Delay(1000);
}
public static void Main()
{
NotTheMainMethod().GetAwaiter().GetResult();
}
public static async Task NotTheMainMethod()
{
await Task.Delay(1000);
}
public static void Main()
{
NotTheMainMethod().GetAwaiter().GetResult();
}
thus because u have that thread anyway, u can also just put it to work for ur synchronous console reads
Bailey
Bailey•6mo ago
Choose you're self what you think is best. The following is just an explenation why I would do the input in a different thread (I would keep the main for controlling everything). If you go to more advanced input ways you will not put the input in the main. Just an example. If you create a windows form app. The main is the program. This starts a form and all textfields input fields use an event trigger. if there is a textfield with refreshing texts this is continioulsy updated from different input parts. In this case the input is also event driven. Now we go further to a website. I will simplify it (without the backend): THe website exists of 2 parts a screen where the messages are shown and an input field. The input is only triggered when a button or an enter is done. The stream of messages keep on refreshing.
Buddy
Buddy•6mo ago
I know with Rust you have a message queue. One channel for receiving the messages and one for sending them. A thread polls the messages and then sends the message to something which acts accordingly.
Bandana Dee(z)
Bandana Dee(z)•6mo ago
its not like it matters since i wont even be using this when it comes to LiteNetLib's actual intended purpose (for i'm pretty sure gamedev) i just wanted to know
Buddy
Buddy•6mo ago
Not sure why you are using such a library for something as simple as a chat application
Bandana Dee(z)
Bandana Dee(z)•6mo ago
getting acquainted i gues
Buddy
Buddy•6mo ago
I suggest using the library for something that requires more packets being sent, such as a real-time collaboration drawing app. A chat app can be made really simple with a basic tcp socket. No need for rUDP and so forth.
Bailey
Bailey•6mo ago
OK
Buddy
Buddy•6mo ago
I used it for a peer to peer collaboration app. It's use is not exclusively for games, but can be adapted to anything.
Bandana Dee(z)
Bandana Dee(z)•6mo ago
sounds difficult but i could try
Buddy
Buddy•6mo ago
If you want to learn networking that is the way. Start with the basics, not a library that practically do everything for you. And it does not need to be async if it is a console. IO (Input Output) is blocking. Console is not asynchronous
ZacharyPatten
ZacharyPatten•6mo ago
You likely don't need to do any asynchronous programming. Just check if there is input via Console.KeyAvailable before calling Console.ReadKey. Then your program won't pause to wait for user input. You will need to add some extra logic because ReadKey only reads one input while ReadLine reads until the next new line, but that extra logic should be pretty trivial.