等待UDPClient.ReceiveAsync与超时 [英] await UDPClient.ReceiveAsync with timeout

查看:2854
本文介绍了等待UDPClient.ReceiveAsync与超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UDPClient像下面

I'm using UDPClient like below

dim c = New UDPClient(port)
client.CLient.ReceiveTimeout = 1
await client.ReceiveAsync()

然而,计谋不会终止或抛出,甚至 虽然我已经设置了超时。这是正常的 行为?

However the await does not terminate or throw even though I have set a timeout. Is this normal behaviour?

推荐答案

这是为Socket.ReceiveTimeout MSDN库文章中明确提到:

It is explicitly mentioned in the MSDN Library article for Socket.ReceiveTimeout:

获取或设置一个值,指定的时间量之后,一个同步接收通话将超时。

Gets or sets a value that specifies the amount of time after which a synchronous Receive call will time out.

着重强调。你是做一个同步的接收相反,当您使用ReceiveAsync()。解决方法是使用您在通话开始前和结束后一个System.Timers.Timer的。关闭插座经过的事件处理程序,以便在ReceiveAsync()方法终止与ObjectDisposed例外。

Emphasis added. You are doing the opposite of a synchronous receive when you use ReceiveAsync(). The workaround is to use a System.Timers.Timer that you start before the call and stop afterwards. Close the socket in the Elapsed event handler so the ReceiveAsync() method terminates with an ObjectDisposed exception.

这篇关于等待UDPClient.ReceiveAsync与超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆