Check EOF in NamedPipeClientStream

Using C-functions, you can check whether the output channel in the pipe is free via _eof(pipeOut) and skip the read operation.

 int endOfFile = _eof(myPipeIn); if(endOfFile != 0) int aReadCount = _read(myPipeIn, aBufferPtr, 256); 

Is it possible to do something similar with .Net NamedPipeClientStream?

+4
source share
2 answers

Unfortunately, the Bueller prompt didn't work for me, because ReadLine might be blocked.

But with Zach’s answer to StreamReader.Peek and Thread.Interrupt Alternative, I came up with the following:

 [DllImport("kernel32.dll", SetLastError = true)] static extern bool PeekNamedPipe(SafeHandle handle, byte[] buffer, uint nBufferSize, ref uint bytesRead, ref uint bytesAvail, ref uint BytesLeftThisMessage); static bool SomethingToRead(SafeHandle streamHandle) { byte[] aPeekBuffer = new byte[1]; uint aPeekedBytes = 0; uint aAvailBytes = 0; uint aLeftBytes = 0; bool aPeekedSuccess = PeekNamedPipe( streamHandle, aPeekBuffer, 1, ref aPeekedBytes, ref aAvailBytes, ref aLeftBytes); if (aPeekedSuccess && aPeekBuffer[0] != 0) return true; else return false; } 

In my case, an extra P / Invoke call is not a problem.

+4
source

According to the documentation http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeclientstream.aspx .Net channels do not have the ability to "peep."

A specific methodology is checking the result of a read operation for NULL.

 using (StreamReader sr = new StreamReader(pipeClient)) { // Display the read text to the console string temp; while ((temp = sr.ReadLine()) != null) { Console.WriteLine("Received from server: {0}", temp); } } 
+1
source

All Articles