We help IT Professionals succeed at work.
Get Started

How do I make this Asyncronous NamedPipe work?

byt3 asked
Last Modified: 2013-12-16
I've done a few days of research attempting to understand how to use NamedPipes asynchronously in C#, but have not had any luck. There are plenty of examples and references, but I don't seem to be understanding them well enough to know what I'm doing wrong, so I am hoping someone will spell it out for me.

Here is the server side code running as a backgroundworker:
private void startSendPipe(object o, DoWorkEventArgs ea) {
            NamedPipeServerStream sendPipe = new NamedPipeServerStream("ShutdownPipeGet",                                           PipeDirection.Out,
                        PipeOptions.Asynchronous); //IPC server sender
            StreamWriter sw;
            IAsyncResult aResult = sendPipe.BeginWaitForConnection(
                delegate(IAsyncResult iar) {
                    try {
                        sw = new StreamWriter(sendPipe);
                        sw.WriteLine(waitTime.ToString());  //send integer converted to string across
                        sendPipe.WaitForPipeDrain();        //wait for client to finish download
                        sw.Close();                         //Close streams when done
                    } catch (IOException ioerr) {
                        Console.WriteLine("Send connection was unexpectedly broken to ShutdownAppGUI.");
                    } catch (Exception err) {
                        Console.WriteLine("Unexpected error on send connection: " + err.Message);
            //keep worker going until either the info has been sent or background worker cancel is requested.
            while (!ea.Cancel && !aResult.IsCompleted) {
            if (!aResult.IsCompleted) { sendPipe.EndWaitForConnection(aResult); }

Open in new window


Here is the client side code running:
NamedPipeClientStream npc = new NamedPipeClientStream(".", "ShutdownPipeGet", PipeDirection.In);    //connect to ShutdownAppCLI(server) to get waitime
            StreamReader sr;
            try {
                sr = new StreamReader(npc);
                String theTime = sr.ReadToEnd();
                waitTime = Int32.Parse(theTime); //converting string to integer
                TimeLabel.Text = waitTime.ToString();
            } catch (Exception err) {
                MessageBox.Show("ShutdownAppGUI Error: " + err.Message);

Open in new window

On the server side I get the error: Pipe hasn't been connected yet.
On the client side I get nothing from the stream.

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE