views:

20

answers:

1

I need to have an anonymous pipe within the same process to allow stream based async communication between two libraries.

Everything works until I call the Stream.Read() - I get a UnauthorizedAccessException - Access to the path is denied. Any ideas?

using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.Out))
using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.In, pipeServer.SafePipeHandle))
{
    // External lib should close server pipe when it's done
    archFile.ExtractionFinished += (sender, args) => pipeServer.Close();
    archFile.BeginExtractFile(archFileName, pipeServer);

    var buf = new byte[1000];

    int read;
    while ((read = pipeClient.Read(buf, 0, buf.Length)) > 0)
    { ... }
}
+1  A: 

You need to use the client constructor that takes the pipe handle as a string rather than a SafeHandle. e.g.:

using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.Out))
using (var pipeClient = new AnonymousPipeClientStream(PipeDirection.In, pipeServer.GetClientHandleAsString()))
{
    //...
}

This is documented in the "Remarks" section at http://msdn.microsoft.com/en-us/library/system.io.pipes.anonymouspipeclientstream.aspx.

Nicole Calinoiu