【发布时间】:2012-05-18 18:31:28
【问题描述】:
如何安全地使以下代码超时?
var client = new TcpClient("localhost", 6000);
new StreamReader(client.GetStream()).ReadLine(); // close the client if no line arrives after 5 seconds...
以前我在另一个线程上运行了一个计时器,如果超时过期,它会调用 client.Close(),但我不认为像这样调用 client.Close() 是线程安全的?
【问题讨论】:
-
为什么不使用 TcpClient.ReceiveTimeout?