Is there a pipe-like .NET Stream class that doesn't use OS protocols?

Using C # /. NET (Xamarin / Mono, really).

I have a class with method (A) that takes a stream that it writes, and I have another class with method (B) that takes a stream to read from.

I want to have a stream that is transmitted both (A) and (B), so when (A) writes to the stream, that data can be read by (B).

Is there already such a beast that does not use OS pipes?

+4
source share
2 answers

I would use a BlockingCollection . for example

BlockingCollection<int> bc = new BlockingCollection<int>();
Task.Run(() => new Reader(bc).DoWork());
Task.Run(() => new Writer(bc).DoWork());

public class Reader
{
    BlockingCollection<int> _Pipe = null;
    public Reader(BlockingCollection<int> pipe)
    {
        _Pipe = pipe;
    }

    public void DoWork()
    {
        foreach(var i in _Pipe.GetConsumingEnumerable())
        {
            Console.WriteLine(i);
        }
        Console.WriteLine("END");
    }
}

public class Writer
{
    BlockingCollection<int> _Pipe = null;
    public Writer(BlockingCollection<int> pipe)
    {
        _Pipe = pipe;
    }

    public void DoWork()
    {
        for (int i = 0; i < 50; i++)
        {
            _Pipe.Add(i);
            Thread.Sleep(100);
        }
        _Pipe.CompleteAdding();
    }
}
+1
source

MemoryStream, ConcurrentMemoryStream.

, . - , .

, : fooobar.com/questions/374586/..., @mike z .

0

Source: https://habr.com/ru/post/1523748/


All Articles