C
C#3w ago
Tijmen

Task cancellation in UDPClient.RecieveAsync not triggering

I'm trying to recieve some UDP data, but give up if it hasnt been gotten within 40 ms. I think the recommended way would be to pass in a TaskCancellationToken where the source has had a call to CancelAfter. However the task doesnt appear to be cancelled at all. When i change my approach and use Task.WhenAny, waiting on either udp or Task.Delay it does work correctly.
CancellationTokenSource src = new CancellationTokenSource();

while (true)
{
src.CancelAfter(40);
var len = await udpClient.Client.ReceiveFromAsync(recieveData, remoteEP, src.Token);
src.TryReset();
}
CancellationTokenSource src = new CancellationTokenSource();

while (true)
{
src.CancelAfter(40);
var len = await udpClient.Client.ReceiveFromAsync(recieveData, remoteEP, src.Token);
src.TryReset();
}
When i disconnect the device i'm connecting to it just hangs on RecieveFrom forever. I tried the overloads in UDPClient and in the underlying socket to no avail. Anything im doing wrong?
1 Reply
qqdev
qqdev3w ago
One issue is that you can’t always reuse the CTS You have to recreate it once a timeout occurred And you have to dispose it TryReset will tell you what to do
Want results from more Discord servers?
Add your server