C#C
C#3y ago
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);
    }
}

the _shouldRun indicates whether the task should continue, I set this to
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?
Was this page helpful?