© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
240 replies
Tim

✅ Cancel stream read

I'm running a Task that constantly reads some data off a (named pipe) stream when it's present.
while (_shouldRun)
{
    // .. the below is in another method, but this is the idea
    using (var reader = new BinaryReader(_receiveStream, encoding, true))
    {
        identifier = reader.ReadUInt16();
        ushort lenght = reader.ReadUInt16();
        data = reader.ReadBytes(lenght);
    }
}
while (_shouldRun)
{
    // .. the below is in another method, but this is the idea
    using (var reader = new BinaryReader(_receiveStream, encoding, true))
    {
        identifier = reader.ReadUInt16();
        ushort lenght = reader.ReadUInt16();
        data = reader.ReadBytes(lenght);
    }
}

the
_shouldRun
_shouldRun
indicates whether the task should continue, I set this to
false
false
when it needs to stop
The issue is that the read operation on the stream is infinitely blocking, so I'm never sure if my Task will end properly

Can I stop a blocking stream read operation from another thread in some way?
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
Next page

Similar Threads

read TCP stream issue
C#CC# / help
4y ago
Read brotli stream in chunks
C#CC# / help
2y ago
WebApplicationFactory stream body read multiple times
C#CC# / help
9mo ago
Read stream twice without missing data
C#CC# / help
3y ago