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?
+1
A:
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 BeginXXXX
to 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.
Daniel Earwicker
2010-02-05 19:16:40
This seems to be a good solution, and I see how to do it with a network stream, but how would I do the same thing with standard in?
Nayruden
2010-02-05 19:31:50
See update (last line)
Daniel Earwicker
2010-02-05 19:35:55