C# Stream.Read with timeout

后端 未结 2 1850
别跟我提以往
别跟我提以往 2020-12-03 17:33

I have this streamreader:

            Boolean read = false;
            while (wline!=\"exit\")
            {

                while (!read || streamReader.P         


        
相关标签:
2条回答
  • 2020-12-03 17:48

    If this is System.IO.StreamReader, then set it on the BaseStream:

    streamReader.BaseStream.ReadTimeout = 2000;  //milliseconds, so 2 seconds
    
    0 讨论(0)
  • 2020-12-03 17:51

    You need to deal with the underlying stream. So, in case you are using a TcpClient, you can simply set the ReceiveTimeout:

    The ReceiveTimeout property determines the amount of time that the Read method will block until it is able to receive data. This time is measured in milliseconds. If the time-out expires before Read successfully completes, TcpClient throws a IOException. There is no time-out by default.

     tcpClient.ReceiveTimeout = 5000;
    
    0 讨论(0)
提交回复
热议问题