10

I have a windows service which is communicating with a gui application via named pipes. Therefor i have a thread running waiting for the app to connect which is running fine if i do it once. But if the thread is creating a new instance of the named pipe stream server the already established connection breaks down and i get the all instances busy exception. The code fragment where the exception is thrown is this:

class PipeStreamWriter : TextWriter
{

    static NamedPipeServerStream _output = null;
    static StreamWriter _writer = null;
    static Thread myThread = null;

        public PipeStreamWriter()
        {
            if (myThread == null)
            {
                ThreadStart newThread = new ThreadStart(delegate{WaitForPipeClient();});
                myThread = new Thread(newThread);
                myThread.Start();
            }
        }

        public static void WaitForPipeClient()
        {
            Thread.Sleep(25000);
            while (true)
            {
                NamedPipeServerStream ps = new NamedPipeServerStream("mytestp");
                ps.WaitForConnection();
                _output = ps;
                _writer = new StreamWriter(_output);

            }
        }

The exception is thrown when creating the new pipe server stream NamedPipeServerStream ps = new NamedPipeServerStream("mytestp") the second time.

EDIT:

I found the answer and it works when the max number of server instances is specified NamedPipeServerStream ps = new NamedPipeServerStream("mytestp",PipeDirection.Out,10);

The default value for this seems to be -1. Which leads to another but not that important question: Someone knows why it is -1 and not 1 when it behaves like beeing 1?

GerS
  • 101
  • 1
  • 5
  • Says [here](https://msdn.microsoft.com/en-us/library/bb355760(v=vs.110).aspx#Anchor_2) the default value is 1. Nonetheless, your solution has solved my problem as well, cheers! I'd consider submitting it as an answer, would've gotten a vote from me. – Drazen Bjelovuk Aug 19 '16 at 04:18
  • I just had a look at the .Net code concerning the "-1" it says: win32 allows fixed values of 1-254 or 255 to mean max allowed by system. We expose 255 as -1 (unlimited) through the MaxAllowedServerInstances constant. This is consistent e.g. with -1 as infinite timeout, etc – DerApe Sep 29 '16 at 11:09
  • Note that [reference source](https://referencesource.microsoft.com/#System.Core/System/IO/Pipes/Pipe.cs,489) also indicates that *MaxAllowedServerInstances* (-1) is the **only** way to specify unlimited. Though -1 is converted to 255 internally, passing 255 is considered invalid and will throw an exception. (Probably how "expose 255 as -1" is to be interpreted, i.e. "only as" not "also as".) – Uber Kluger Jan 04 '22 at 01:00

1 Answers1

2

There are two overloads of the NamedPipeServerStream constructor that assign a default to the maxNumberOfServerInstances variable, namely:

public NamedPipeServerStream(String pipeName)

and

public NamedPipeServerStream(String pipeName, PipeDirection direction)

Looking at the reference source proves that this default is 1 and not -1. This explains the behavior you observed.

Possible solutions are:

  1. use a constructor that allows you to specify the limit, and pass a value larger than 1

  2. same as 1, and use the built-in constant NamedPipeServerStream.MaxAllowedServerInstances to ask for the maximum number of handles the operating system will be able to allocate.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77