© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•15mo ago•
4 replies
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EventHandler not triggering.
C#CC# / help
3y ago
Cancellation Tokens
C#CC# / help
2y ago
✅ @onchange event not triggering
C#CC# / help
3y ago