© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•5mo ago•
3 replies
DaVinki

Threadsafe async code design

With a single UDP/TCP client or serial connection, I want to send and receive concurrently. The sending and receiving code is asynchronous so locks are out of the question. Trying to follow closely with a hosted app and dependency injection. Been stuck thinking about it for a few hours because every solution I've been able to come up has a gotcha.

Attached is the logic

Thought splitting into two classes would be good but if I'm working with the same UDP/TCP/serial connection then it goes back to the locking issue. Adding a third class for message queueing could work but then you might end up waiting for bytes with outgoing messages in queue that could've been sent by the time you receive the next bytes.
GroundCompanion.cs2.16KB
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

✅ How to make httpcontext threadsafe?
C#CC# / help
11mo ago
✅ convert synchronous code to async?
C#CC# / help
12mo ago
❔ Async Method with current code.
C#CC# / help
3y ago
✅ Debugging async code in vscode
C#CC# / help
3y ago