I’m trying to make a networked console based application, but it needs to be able to listen to standard input and input from a socket at the same time. In C++ I would use the posix select() function to do this, but in C# it appears that the equivalent select function is for sockets only. Is there a way I can listen to both inputs in C# without resorting to multiple threads?
Share
To wait on multiple inputs, you need a WaitHandle for each one, and then you call the static method WaitHandle.WaitAny.
But another option is to use async IO. Use the
BeginXXXXto start read/receive operations. You supply a callback in each case which will be executed on completion. After you launch them, you wait on a monitor object, and in the callbacks you pulse that monitor object to notify completion. This is a very efficient form of multi-threading programming but you don’t have to start any threads explicitly.To get a raw Stream for standard input, use Console.OpenStandardInput.