2011-07-27 6 views
4

C関数を使用すると、パイプの出力側が空であるかどうかを_eof(pipeOut)で確認し、読み取り操作をスキップできます。NamedPipeClientStreamでEOFを確認する

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

.NetのNamedPipeClientStreamと同様のことが可能ですか?

答えて

4

残念ながらBuellerのヒントは、私のために動作しませんでした。

しかし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 /呼び出しコールは問題ありません。

+0

SafeHandle streamHandleをCreateFileまたはCreateNamedPipeで取得しましたか? –

1

ドキュメントhttp://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeclientstream.aspxによると、.Netパイプで利用可能な "ピーク"タイプの機能はありません。

特定された方法論は、NULLの読み取り操作の結果をテストすることです。 ReadLineをブロックすることができますので、

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); 
       } 
      } 
+0

ありがとうございました!コードを変更して、 'StreamReader'の解決策と動作するようにします。 –