title: Using named pipes, how to write messages client receives in its form one by one?
body: <p>I have a Windows service and a client listening with the following funcionality:</p>
  
  <p>Win service:</p>
  
  <ol>
  <li>Creates named pipe</li>
  <li>Creates client process</li>
  <li>Waits for connection</li>
  <li>Writes to client process</li>
  <li>Reads from client</li>
  <li>Writes to client process</li>
  <li>Reads from client</li>
  <li><p>...</p>
  
  <pre><code>public static bool StartProcessAsCurrentUser()
  {
      var hUserToken = IntPtr.Zero;
      var sInfo = new STARTUPINFO();
      var procInfo = new PROCESS_INFORMATION();
      var pEnv = IntPtr.Zero;
      int iResultOfCreateProcessAsUser;
      string cmdLine = "ClientNamedPipeForm.exe";
  
      sInfo.cb = Marshal.SizeOf(typeof(STARTUPINFO));
      byte[] buffer = new byte[BUFSIZE];
  
      try
      {
          var tSecurity = new SECURITY_ATTRIBUTES();
          tSecurity.nLength = Marshal.SizeOf(tSecurity);
          var pSecurity = new SECURITY_ATTRIBUTES();
          pSecurity.nLength = Marshal.SizeOf(pSecurity);
          pSecurity.bInheritHandle = true; //For controling handles from child process
  
          IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(pSecurity));
          Marshal.StructureToPtr(pSecurity, pointer, true);
  
          PipeSecurity ps = new PipeSecurity();
          System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
          PipeAccessRule par = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
          ps.AddAccessRule(par);
          NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.WriteThrough, 10, 10, ps);
  
          StreamWriter sw = new StreamWriter(pipeServer);
  
          if (!CreateProcessAsUser(hUserToken,
              null, // Application Name
              cmdLine, // Command Line
              IntPtr.Zero,
              IntPtr.Zero,
              true,
              dwCreationFlags,
              pEnv,
              null, // Working directory
              ref sInfo,
              out procInfo))
          {
              throw new Exception("StartProcessAsCurrentUser: CreateProcessAsUser failed.\n");
          }
  
          try
          {
                  pipeServer.WaitForConnection();
                  sw.WriteLine("Waiting");
                  sw.Flush();
                  pipeServer.WaitForPipeDrain();
  
                  Thread.Sleep(5000);
                  sw.WriteLine("Waiting2");
                  sw.Flush();
                  pipeServer.WaitForPipeDrain();
  
                  Thread.Sleep(5000);
                  sw.WriteLine("Waiting32");
                  sw.Flush();
                  pipeServer.WaitForPipeDrain();
  
                  Thread.Sleep(5000);
                  sw.WriteLine("QUIT");
                  sw.Flush();
                  pipeServer.WaitForPipeDrain();
          }
          catch (Exception ex) { throw ex; }
  
          finally
          {
                  if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
          }
      }
      finally
      {
          //Closing things
      }
  
      return true;
  }
  </code></pre></li>
  </ol>
  
  <p>Client:</p>
  
  <ol>
  <li>Creates named pipe</li>
  <li>Connects</li>
  <li>Reads from service</li>
  <li>Writes in its Form</li>
  <li>Writes to service</li>
  <li>Reads from service</li>
  <li>Writes in its Form</li>
  <li>Writes to service</li>
  <li><p>...</p>
  
  <pre><code>private void Client()
  {
    try
    {
      IntPtr hPipe;
      string dwWritten;
      byte[] buffer = new byte[BUFSIZE];
  
      NamedPipeClientStream pipeClient = new NamedPipeClientStream(".","testpipe", PipeDirection.In, PipeOptions.WriteThrough);
  
      if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
  
      StreamReader sr = new StreamReader(pipeClient);
  
      string temp;
      bool cont = true;
      while (cont)
      {
          temp = "";
          temp = sr.ReadLine();
          if (temp != null)
          {
              listBox1.Items.Add(temp);
              listBox1.Refresh();
          }
          if (temp != "QUIT")
          {
              sw.WriteLine("Response");
              sw.Flush();
              pipeClient.WaitForPipeDrain();
          }
          else
          {
              sw.WriteLine("Response");
              cont = false;
          }
      }
    }
    catch (Exception ex)
    {
        throw new Exception("Exception: " + ex.Message);
    }
  </code></pre></li>
  </ol>
  
  <p>The problem appears writting to <code>listbox1</code>. Form (and its <code>listbox1</code>) only appears on user screen when the whole process has ended, and it shows the four message at once. I have <code>Thread.Sleep(5000)</code> in service side in order to evidence that each message is written separately, but I'm not sure if process doesn't wait to <code>Thread</code> and I'm testing it wrongly or Form is shown with all messages at once by some reason...</p>