UdpClient.ReceiveAsync correct early termination

后端 未结 2 650
情歌与酒
情歌与酒 2020-12-17 05:58

Good day. I work with UdpClient and have wrapper upon it.

For reading I have asynchronous method:

private async Task Recei         


        
2条回答
  •  星月不相逢
    2020-12-17 06:32

    The only way to cancel a pending receive is to disconnect/stop/dispose as you did. This is correct. You need to catch and ignore that exception.

    It is an unfortunate design problem with the .NET Framework that this is the only way to do it.

    Note, that WithCancellation does not cancel the IO. The Receive is still running. That's why WithCancellation must be followed by disposing of the socket to make sure that there are no further pending IOs.

提交回复
热议问题