문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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. The ConcurrentQueue class makes it very simple.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top