I cannot find a good example of how to create a multitask named pipe listener that runs asynchronously. I can do a second listen:
NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut); while (true) { pipeServer.WaitForConnection(); StreamReader reader = new StreamReader(pipeServer); MessageBox.Show(reader.ReadLine()); pipeServer.Disconnect(); }
and I can make an asynchronous listener:
NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); pipeServer.BeginWaitForConnection((a) => { pipeServer.EndWaitForConnection(a); StreamReader reader = new StreamReader(pipeServer); MessageBox.Show(reader.ReadLine()); }, null);
But I don't seem to go. Is there a good example for this? I am also concerned about partially sent messages, as I believe this is a problem with asynchronous messages like this.
Update : I'm a little closer.
pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); pipeServer.BeginWaitForConnection((a) => { pipeServer.EndWaitForConnection(a); StreamReader reader = new StreamReader(pipeServer); while (running) { String text = reader.ReadLine(); if (String.IsNullOrEmpty(text) == false) { MessageBox.Show(text); } } MessageBox.Show("Done!"); }, null);
This will be read successfully once and continue the loop when ReadLine returns an empty blank line after the initial successful read. Thus, it clearly does not block and tries to read again. The problem is that if I send the same message a second time, it will not be picked up, and my trumpeter says that he is getting error 2316 (although I cannot understand what this means). I think I just need to do something similar to this, when the pipe will be cleaned every time, like the first code sample that I listed, but I have not received this to work.
Mike pateras
source share