Continuously reading from a stream in C#?

Posted by Damien Wildfire on Stack Overflow See other posts from Stack Overflow or by Damien Wildfire
Published on 2010-05-06T22:13:24Z Indexed on 2010/05/06 22:18 UTC
Read the original article Hit count: 162

Filed under:
|

I have a Stream object that occasionally gets some data on it, but at unpredictable intervals. Messages that appear on the Stream are well-defined and declare the size of their payload in advance (the size is a 16-bit integer contained in the first two bytes of each message).

I'd like to have a StreamWatcher class which detects when the Stream has some data on it. Once it does, I'd like an event to be raised so that a subscribed StreamProcessor instance can process the new message.

Can this be done with C# events without using Threads directly? It seems like it should be straightforward, but I can't get quite get my head around the right way to design this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about events