Implementing a timeout with NetworkStream.BeginRead and NetworkStream.EndRead
问题 I've written the following function to implement a timeout feature using NetworkStream 's asynchronous read functions ( BeginRead and EndRead ). It works fine until I comment out the line Trace.WriteLine("bytesRead: " + bytesRead); . Why? private int SynchronousRead(byte[] buffer, int count) { int bytesRead = 0; bool success = false; IAsyncResult result = null; result = _stream.BeginRead( buffer, 0, count, delegate(IAsyncResult r) { bytesRead = _stream.EndRead(r); }, null); success = result