title: Multi-threaded Named Pipe Server
body: <p>I'm trying to create a Multi-Threaded Named Pipe Server which can handle multiple requests in parallel.  See below for my code.</p>
  <p>The behaviour is not as expected.  If new requests come in while the current one is busy it throws &quot;access to the path is denied&quot; error until the current executing thread is released.</p>
  <p>Any idea what could be the problem ?</p>
  <pre><code>using Newtonsoft.Json;
  using System;
  using System.IO;
  using System.IO.Pipes;
  using System.Security.AccessControl;
  using System.Security.Principal;
  using System.Text;
  using System.Threading;
  
  namespace API.PipeServer
  {
      public class Server
      {
          private bool running;
          private Thread runningThread;
          private EventWaitHandle terminateHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
          public string PipeName { get; set; }
  
          private void ServerLoop()
          {
              while (running)
              {
                  ProcessNextClient();
              }
  
              terminateHandle.Set();
          }
  
          public void Run()
          {
              running = true;
              runningThread = new Thread(ServerLoop);
              runningThread.Start();
          }
  
          public void Stop()
          {
              running = false;
              terminateHandle.WaitOne();
          }
  
          public virtual string ProcessRequest(string message)
          {
              return &quot;&quot;;
          }
  
          public void ProcessClientThread(object o)
          {
              using (NamedPipeServerStream pipeStream = (NamedPipeServerStream)o)
              {
                  StreamReader sr = new StreamReader(pipeStream);
  
                  //Receive instruction message
                  string instructionJson = sr.ReadLine();
                  Console.WriteLine($&quot;{DateTime.Now.ToString(&quot;yyyy-MM-dd HH:mm:ss&quot;)}: --- Receiving Instruction ---&quot;);
                  Console.WriteLine(instructionJson);
  
                  //Getting instruction message
                  var instruction = JsonConvert.DeserializeObject&lt;InstructionHandler.Instruction&gt;(instructionJson);
  
                  //Get data
                  var json = API.API.GetAPIJson(instruction);
  
                  //Send data back to Python client
                  try
                  {
                      pipeStream.Write(Encoding.UTF8.GetBytes(json));
                      pipeStream.WaitForPipeDrain();
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex);
                  }
  
                  Console.WriteLine($&quot;{DateTime.Now.ToString(&quot;yyyy-MM-dd HH:mm:ss&quot;)}: --- Closing Stream ---&quot;);
  
                  pipeStream.Close();
                  pipeStream.Dispose();
              }
          }
  
          public void ProcessNextClient()
          {
              try
              {
                  Console.WriteLine($&quot;{DateTime.Now.ToString(&quot;yyyy-MM-dd HH:mm:ss&quot;)}: Creating server&quot;);
                  PipeSecurity pipeSecurity = new PipeSecurity();
                  pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
  
                  var pipeStream = NamedPipeServerStreamAcl.Create(&quot;test_api&quot;, PipeDirection.InOut, 254, PipeTransmissionMode.Message, PipeOptions.None, 1024, 1024, pipeSecurity);
  
                  pipeStream.WaitForConnection();
  
                  //Spawn a new thread for each request and continue waiting
                  Thread t = new Thread(ProcessClientThread);
                  t.Start(pipeStream);
              }
              catch (Exception e)
              {
                  Console.WriteLine($&quot;{DateTime.Now.ToString(&quot;yyyy-MM-dd HH:mm:ss&quot;)}: Error - {e.Message}&quot;);
              }
          }
      }
  }
  </code></pre>
  <p>Below is a typical output:</p>
  <p><a href="https://i.stack.imgur.com/1IbWP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1IbWP.png" alt="" /></a></p>