views:

145

answers:

2

Hi,

I am trying to figure out how to pass a class/struct/etc.. using named pipe between thread(I am trying to measure some performance using the stopwatch and compare it to other methods..)

Anyway , All the documention I've found is talking about using StreamReader and readline to get the data from the NamedPipeServerStream. However readline is a string, how do I actually use the data from the named pipe if I am passing something that is not a string.

Thanks, Eyal

+2  A: 

NamedPipeServerStream is a stream - so it's fine for binary data out of the box. Just treat it like a normal stream, rather than wrapping it in a StreamReader.

As for passing objects - why used named pipes if you're strictly within a single process? Just create an in-memory producer/consumer queue.

Jon Skeet
Thanks. I've been doing some measurement with producer/consumer queue and it take 12msec-34msec(my CPU is Intel Q9550) to get my message into my thread. I was thinking that because the namedpipe is implemented in the windows kernel maybe there is some chance it will be faster.
Eyalk
It sounds like your producer/consumer implementation is broken then. It should be *much, much* faster than that.
Jon Skeet
+1  A: 

Using BinaryFormatter is the easiest way to serialize an object in and out of a pipe stream. You'll need to decorate the class or struct with the [Serializable] attribute.

Using a pipe is however a very inefficient way to "pass" data between threads. Every thread in a process has access to the same garbage collected heap, no serialization is required as long as the threads run in the same AppDomain. You do need to synchronize access to the object(s), use the lock statement.

Hans Passant
thanks. I did not know about the BinaryFormatter class. now I know :)
Eyalk