Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program, that has to comunicate with another program that is called from the first.
I have managed to send the required data from the first to the second program using NamedPipes.
When the second program closes, i need to send some data back to the first program. I set up a NamedPipeServerStream in the first program and from the Closing event of the second program a NamedPipeClientStream. As far as i can tell the syncronization of the pipes dosen't work properly but i don't know why.

This is the code from the first program:
private const string CLIENTPROC = "C:\\Users\\Maik\\Source\\Repos\\PipeTest\\PipeClient\\obj\\x86\\Release\\PipeClient.exe";
private ManualResetEvent threadResetEvent;
private Thread threadHandlePipeSendLast;
private Process procClient;
private string msgPipeSend;
private volatile bool bMsgPipeSend;
public MainWindow()
{
    InitializeComponent();
    this.threadResetEvent = new ManualResetEvent(false);
    System.Diagnostics.Debug.WriteLine("### starting thread");
    this.threadHandlePipeSendLast = new Thread(new ThreadStart(this.ExchangeMapOverlayFileServer));
    this.threadHandlePipeSendLast.Start();
}

private void ExchangeMapOverlayFileServer()
{
    System.Diagnostics.Debug.WriteLine("server thread started");
    Thread.CurrentThread.Name = "apocalypse";
    try
    {
        using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("ClosingPipe", PipeDirection.In, 1, PipeTransmissionMode.Byte))
        {
            string line;

            namedPipeServer.WaitForConnection();
            StreamReader sr = new StreamReader(namedPipeServer);
            Thread.Sleep(100);
            line = sr.ReadLine();
            handleRecvMsgFromPipe(line);
            namedPipeServer.Disconnect();
            namedPipeServer.Close();
        }
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine("### " + e.Message + "\n" + e.StackTrace);
    }
}

private void handleRecvMsgFromPipe(string line)
{
    this.outbox.Text = line;
}

private void buttonOpenFormClient_Click(object sender, EventArgs e)
{
G


    if (this.procClient == null)
    {
        try
        {
            this.procClient = Process.Start(new ProcessStartInfo(CLIENTPROC));
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message, "Error");
        }
    }

}


this is the code from the second program

private ManualResetEvent threadResetEvent;
private Thread threadHandlePipeSendLast;

private string msgPipeSend;
private volatile bool bMsgPipeSend;
private bool done;

public MainWindow()
{
    InitializeComponent();
    this.threadResetEvent = new ManualResetEvent(false);
    this.Closing += (s, a) =>
    {
        System.Diagnostics.Debug.WriteLine("+++ FormClosing started.");
        this.threadHandlePipeSendLast = new Thread(new ThreadStart(this.HandleWindowClosing));
        this.threadHandlePipeSendLast.Start();
        while (!done)
        {
            Thread.Sleep(1000);
        }
    };
}

private void HandleWindowClosing()
{
    try
    {
        using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream(".", "ClosingPipe", PipeDirection.Out))
        {
            namedPipeClient.Connect();
            StreamWriter sw = new StreamWriter(namedPipeClient);
            //sw.AutoFlush = true;
            sw.WriteLine("last message");
            namedPipeClient.WaitForPipeDrain();
            namedPipeClient.Close();
            this.done = true;
        }

    }
    catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message + "\n" + e.StackTrace); }
}



Where is my mistake?
Thank you.

What I have tried:

I tried:
- using WaitForPipeDrain() which is simply ignored/ dosen't do anything
- to manage the Threads with Sleep() at various points in the code
- to switch Server and Client
- Pipedirection.InOut on both ends
- Client with PipeDirection.Out, Server with PipeDirection.In (which is what i actually need)
Posted
Updated 20-Aug-19 2:13am
v2

1 solution

Maybe you can try WCF, I once used it for two way communicate between a tray-app and a Windows service, and that worked without any problems: NetNamedPipeBinding | Microsoft Docs[^]
 
Share this answer
 
Comments
Myrkjartan Hrolfr 21-Aug-19 2:58am    
Thank you for your answer. In the meantime i solved the problem by cirumventing the closing event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900