I'm trying to send bytes between two C# Actions using Stream.Synchronized to wrap a MemoryStream. Every time the consumer does a ReadByte, it always gets a -1, indicating the stream is closed. When I step thru with the debugger, the producer is calling WriteByte. I never get any data on the consumer action.
static void Main(string[] args)
{
var memStream = new MemoryStream(100);
Stream dataStream = Stream.Synchronized(memStream);
Action producer = () =>
{
for (int i = 0; i < 256; i++)
{
dataStream.WriteByte(Convert.ToByte(i));
dataStream.Flush();
}
};
int total = 0;
Action consumer = () =>
{
int b;
do
{
b = dataStream.ReadByte();
if (b>=0)
total += b;
}
while (b < 255);
};
Parallel.Invoke(producer, consumer);
Console.Out.WriteLine("Total = {0}", total);
}