Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:15:23+00:00 2026-06-07T13:15:23+00:00

My previous question on the same theme: C#: Asynchronous NamedPipeServerStream understanding Now I have

  • 0

My previous question on the same theme: C#: Asynchronous NamedPipeServerStream understanding
Now I have next:

private void StartListeningPipes()
{
    try
    {
        isPipeWorking = true;
                namedPipeServerStream = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE);
                Console.Write("Waiting for client connection...");
                while(isPipeWorking)
                {
            IAsyncResult asyncResult = namedPipeServerStream.BeginWaitForConnection(this.WaitForConnectionAsyncCallback, null);
                        Thread.Sleep(3*1000);
                }
        }
        //// Catch the IOException that is raised if the pipe is broken or disconnected.
        catch (IOException e)
        {
        Console.WriteLine("IOException: {0}. Restart pipe server...", e.Message);
                StopListeningPipes();
                StartListeningPipes();
        }
        //// Catch ObjectDisposedException if server was stopped. Then do nothing.
        catch (ObjectDisposedException)
        {
        }
}

private void WaitForConnectionAsyncCallback(IAsyncResult result)
{
    try
    {
        namedPipeServerStream.EndWaitForConnection(result);
        Console.WriteLine("Client connected.");
        namedPipeServerStream.WaitForPipeDrain();
                byte[] buff = new byte[BUFFERSIZE];
                namedPipeServerStream.Read(buff, 0, BUFFERSIZE);
                string recStr = TrimNulls(buff);
                Array.Clear(buff, 0, buff.Length);
                Console.WriteLine();
                Console.WriteLine("'"+recStr+"'");
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.Message);            
        }
}

But I’m getting

The pipe is being closed Exception everytime I receive a message from client

Why?

My client:

 using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(General.PIPENAME))
{
    try
        {
        byte[] bytes = General.Iso88591Encoding.GetBytes(sendingMessage);
                pipeStream.Write(bytes, 0, bytes.Length);
                pipeStream.Flush();
                pipeStream.WaitForPipeDrain();
        }
        catch (TimeoutException)
        {
        Console.WriteLine("Timeout error!");
        }
    catch (Exception e)
        {
        Console.WriteLine(string.Format("Error! ", e.Message));
        }
}

Final code at the moment is:

/// <summary>
        /// Create new NamedPipeServerStream for listening to pipe client connection
        /// </summary>
        private void ListenForPipeClients()
        {
            if (!this.isListeningToClients)
                return;

            try
            {
                PipeSecurity ps = new PipeSecurity();
                PipeAccessRule par = new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
                ps.AddAccessRule(par);
                pipeClientConnection = new NamedPipeServerStream(General.PIPENAME, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, General.BUFFERSIZE, General.BUFFERSIZE, ps);
                Console.Write("Waiting for client connection...");
                /*namedPipeServerStream.WaitForConnection();
                OnPipeConnected(namedPipeServerStream);*/
                IAsyncResult result = pipeClientConnection.BeginWaitForConnection(OnPipeConnected, pipeClientConnection);
            }
            catch (ObjectDisposedException)
            {
                //// Catch ObjectDisposedException if server was stopped. Then do nothing.
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occures: {0}. Restart pipe server...", e.Message);
                this.logger.Add(LogLevel.Warning, string.Format("Error occures: {0}. Restart pipe server...", e.Message));
                ListenForPipeClients();
            }
        }

        /// <summary>
        /// Async callback on client connected action
        /// </summary>
        /// <param name="asyncResult">Async result</param>
        private void OnPipeConnected(IAsyncResult asyncResult)
        {
            using (var conn = (NamedPipeServerStream)asyncResult.AsyncState)
            {
                try
                {
                    conn.EndWaitForConnection(asyncResult);
                    Console.WriteLine("Client connected.");
                    PipeClientConnection clientConnection = new PipeClientConnection(conn, notifierSenderCache, defaultStorageTime);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    this.logger.Add(LogLevel.Warning, e.Message);
                }
            }

            ListenForPipeClients();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:15:25+00:00Added an answer on June 7, 2026 at 1:15 pm

    It appears that you need a separate NamedPipeServerStream for each client. (Note that I was not the one to discover this, see the other answers.) I’d imagine the working server-side would look something like this (draft code):

    while(this.isServerRunning)
    {
         var pipeClientConnection = new NamedPipeServerStream(...);
    
         try
         {
             pipeClientConnection.WaitForConnection();
         }
         catch(...)
         {
             ...
             continue;
         }
    
         ThreadPool.QueueUserWorkItem(state =>
              {
                   // we need a separate variable here, so as not to make the lambda capture the pipeClientConnection variable, which is not recommended in multi-threaded scenarios
                   using(var pipeClientConn = (NamedPipeServerStream)state)
                   {
                        // do stuff
                        ...
                   }
              }, pipeClientConnection);
    }
    

    As a side note, as it was pointed out in a comment to your question, you’re wasting memory with initiating a new async call every 3 seconds by calling BeginWaitForConnection in a loop (the only case where this wouldn’t waste memory is when new connections are made in intervals smaller than 3 seconds, but I doubt that you can know this for sure). You see, basically every 3 seconds you’re initiating a new async call, regardless of whether the last one is still pending or has completed. Furthermore, it – once again – does not take into account that you need a separate NamedPipeServerStream for each client.

    To fix this issue, you need to eliminate the loop, and “chain” the BeginWaitForConnection calls using the callback method. This is a similar pattern you’ll see quite often in async I/O when using .NET. Draft code:

    private void StartListeningPipes()
    {
        if(!this.isServerRunning)
        {
            return;
        }
    
        var pipeClientConnection = new NamedPipeServerStream(...);
    
        try
        {
            pipeClientConnection.BeginWaitForConnection(asyncResult =>
                {
                    // note that the body of the lambda is not part of the outer try... catch block!
                    using(var conn = (NamedPipeServerStream)asyncResult.AsyncState)
                    {
                        try
                        {
                            conn.EndWaitForConnection(asyncResult);
                        }
                        catch(...)
                        {
                            ...
                        }
    
                        // we have a connection established, time to wait for new ones while this thread does its business with the client
                        // this may look like a recursive call, but it is not: remember, we're in a lambda expression
                        // if this bothers you, just export the lambda into a named private method, like you did in your question
                        StartListeningPipes();
    
                        // do business with the client
                        conn.WaitForPipeDrain();
                        ...
                    }
                }, pipeClientConnection);
        }
        catch(...)
        {
            ...
        }
    }
    

    The control flow will be something like this:

    • [main thread] StartListeningPipes(): created NamedPipeServerStream, initiated BeginWaitForConnection()
    • [threadpool thread 1] client #1 connecting, BeginWaitForConnection() callback: EndWaitForConnection() then StartListeningPipes()
    • [threadpool thread 1] StartListeningPipes(): created new NamedPipeServerStream, BeginWaitForConnection() call
    • [threadpool thread 1] back to the BeginWaitForConnection() callback: getting down to business with the connected client (#1)
    • [threadpool thread 2] client #2 connecting, BeginWaitForConnection() callback: …
    • …

    I think that this is a lot more difficult than using blocking I/O – in fact, I’m not quite certain I got it right, please point it out if you see any mistakes – and it’s also a lot more confusing.

    To pause the server in either examples, you obviously would set the this.isServerRunning flag to false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on my previous question , I've trying now to have them in the
This is very similar to a previous question (and may be the exact same
This question is related to my previous question . The storyline: I have an
In contrast to my previous question , i'll try to give my requirements. I
This is connected with my previous question as it's dealt with the same piece
NOTE: This is the simple version of my previous question that SHOULD have been
This is an extension to a previous question which was answered. But have a
I have this function in R from a previous question here shift <- function(d,
(For those who read my previous question, this is the same teacher and the
So using the advice found in my previous ' question ', I have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.