2

I am having some hard time understanding how to work with streams in C#.

I plan to have a class of this form:

class Pipe {
    public void PutChar(char c) { ... }
    public char GetChar() { ... }
}

The idea is that one thread will put chars in this Pipe through its PutChar() method, and later on other thread will make use of GetChar() to get the chars that are in Pipe.

In Java I'd make use of PipedReader and PipedWriter classes. There seem to be no equivalent classes in C#, so which (stream?) classes should I use here? Or aren't streams the correct way of implementing this? Maybe I'd be better off using a Queue, instead?

Thanks

jzd
  • 23,473
  • 9
  • 54
  • 76
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • I'm not clear if you are looking for the specific named pipes OS implementation, as answered by others. Perhaps you are looking at pipes in a generic sense? I – kenny Dec 27 '10 at 02:30

5 Answers5

4

Because you want to communicate across threads and you don't want to use any NamedPipes as mentioned in above answers than

1- You can create Producer Consumer Queue.

2- If you are using .net 4.0 than ConcurrentQueue can be used.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
1

Look in the System.IO.Pipes namespace for classes that can stream to pipes. However, you may want to consider WCF using NetNamedPipeBinding for implementing pipes in your application.

  • That seems way "heavier" than what I pretend. I just want a way to communicate across threads, not across processes. – devoured elysium Dec 27 '10 at 02:40
  • Why do you need to communicate across threads? Is one thread a worker of some sort? –  Dec 27 '10 at 02:42
  • Yes, the "left" thread is a producer and the "right" one is a consumer. – devoured elysium Dec 27 '10 at 02:47
  • 2
    Unless you specifically need to control the worker thread, just queue operations with the ThreadPool class: http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx. You can do `ThreadPool.QueueUserWorkItem(SomeMethod);` or `ThreadPool.QueueUserWorkItem(obj => { Console.WriteLine(obj); });` as examples. –  Dec 27 '10 at 02:56
1

If you are using .NET 4.0 take a look at the BlockingCollection and the Task Parallel Library. Those two put together create a great way to move data across threads using Tasks.

Example code on MSDN shows everything you need to do.

Ryan Pedersen
  • 3,177
  • 27
  • 39
0

Take a look at the NamedPipe's. Also take a look at this post.

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
-1

StreamReader & StreamWriter are very similar to PipedReader & PipedWriter. If you declare a MemoryStream to hold the buffer you can implement it.

A quick code sample is below which may do what you are looking for.

class Pipe {
    MemoryStream _ms;
    StreamReader _sr;
    StreamWriter _sw;

    public Pipe()
    {
       _ms = new MemoryStream();
       _sr = new StreamReader(_ms);
       _sw = new StreamWriter(_ms);
    }

    public void PutChar(char c) { _sw.Write(c) }
    public char GetChar() { return _sw.Read();  }
}
AdamSane
  • 2,831
  • 1
  • 24
  • 28
  • Maybe you mean _sr.Read() ? Either way it doesn't seem to work :( Tried adding some data to the Pipe and then using GetChar() to get it and all I see are -1's. – devoured elysium Dec 27 '10 at 02:46
  • how is you instantiating and calling the methods? Are you making sure your threads uses the same instance of the Pipe class? – Pauli Østerø Dec 27 '10 at 03:01
  • 2
    I think the MemoryStream only maintains one current position, not separate ones for read and write. – Peter Dec 27 '10 at 03:19