How can I safely timeout the following code?
var client = new TcpClient("localhost", 6000);
new StreamReader(client.GetStream()).ReadLine(); // close the client if no line arrives after 5 seconds...
Previously I had a timer running on another thread which called client.Close() if the timeout expired, but I don't think calling client.Close() like that is thread-safe?
You can use the builtin ReceiveTimeout property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With