使用 C 函数可以通过 _eof(pipeOut)
检查管道的输出端是否为空并跳过读取操作。
int endOfFile = _eof(myPipeIn);
if(endOfFile != 0)
int aReadCount = _read(myPipeIn, aBufferPtr, 256);
是否可以使用 .Net 的 NamedPipeClientStream 做类似的事情?
最佳答案
不幸的是,Bueller 的提示对我不起作用,因为 ReadLine
可以阻塞。
但是 Zach 在 Alternative to StreamReader.Peek and Thread.Interrupt 上的回答我想出了以下内容:
[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;
}
在我的例子中,额外的 P/Invoke 调用没有问题。
关于c# - 检查 NamedPipeClientStream 中的 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846365/