Vinusha Perera
Vinusha Perera

Reputation: 68

C# WPF Anonymous Pipes AnonymousPipeClientStream pipe direction out doesn't work

I'm developing a test application to communicate between 2 processes using Anonymous Pipes. I can send a message to the ClientView from the ServerView but I can't do the other way round since the pipe connection throws exceptions.

ServerCode

 public class PipeServerHandler
{
    private static AnonymousPipeServerStream serverSenderStream;
    private static AnonymousPipeServerStream serverReceiverStream;
    private static StreamWriter sw;
    private static StreamReader sr;


    public void ServerStart()
    {
        serverSenderStream = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
        serverReceiverStream = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);

        string senderID = serverSenderStream.GetClientHandleAsString();
        string receiverID = serverReceiverStream.GetClientHandleAsString();

        ProcessStartInfo process = new ProcessStartInfo(@"C:\Users\VinushaP\Documents\Visual Studio 2015\Projects\CommunicationProject\ClientView\bin\Debug\ClientView.exe", senderID + "_" + receiverID);
        process.UseShellExecute = false;

        Process clientProcess = Process.Start(process);

        serverSenderStream.DisposeLocalCopyOfClientHandle();

        sr = new StreamReader(serverReceiverStream);
    }

    public string ReadData()
    {
        string temp;
        temp = sr.ReadLine();

        return temp;
    }
}

Client Code

public class PipeClientHandler
{

    private static AnonymousPipeClientStream clientReceiverStream;
    private static AnonymousPipeClientStream clientSenderStream;
    private static StreamReader sr;
    private static StreamWriter sw;

    public void ClientStart(string[] args)
    {
        string[] id = args[0].Split('_');
        string senderID = id[0];
        string receiverID = id[1];

        clientReceiverStream = new AnonymousPipeClientStream(PipeDirection.In, senderID);
        clientSenderStream = new AnonymousPipeClientStream(PipeDirection.Out, receiverID);

        sw = new StreamWriter(clientSenderStream);
        sw.AutoFlush = true;
    }

    public void WriteStream(string data)
    {
        sw.WriteLine(data);
        clientSenderStream.WaitForPipeDrain();
    }

}

This is the app.xaml.cs class which captures the Command Line args

private void Application_Startup(object sender, StartupEventArgs e)
    {
        PipeClientHandler handler = new PipeClientHandler();

        if (e.Args.Length > 0)
        {
            handler.ClientStart(e.Args);
        }
    }

When sending data from PipeServerHandler to PipeClienHandler it works well, but when I send data from PipeClientHandler to PipeServerHandler it doesn't show me any messages.

clientSenderStream error

enter image description here

Upvotes: 0

Views: 982

Answers (1)

Mgamerz
Mgamerz

Reputation: 2900

Anonymous pipes are one way. You'll need two anonymous pipes to do 2 way communications.

Upvotes: 2

Related Questions