Cancel C# 4.5 TcpClient ReadAsync by timeout

前端 未结 4 459
北海茫月
北海茫月 2021-01-11 23:32

What would the proper way to cancel TcpClient ReadAsync operation by timeout and catch this timeout event in .NET 4.5?

TcpClient.ReadTimeout seems to be applied to

4条回答
  •  时光说笑
    2021-01-11 23:58

    You have to implement it by yourself: it is not available in that class.

    you should use a timer that will kill the async operation after a timespan or use the Read sync version in a a backgroundthread.

    there you can find an example: Implementing a timeout with NetworkStream.BeginRead and NetworkStream.EndRead

提交回复
热议问题