title: Best way to send and receive messages between a Windows service and form application
body: <p>I'm attempting to implement IPC, but what I'm doing is somewhat different from the documentation I've seen.</p>
  
  <p>I need to asynchronously send, receive, and respond with a service and form application.</p>
  
  <pre><code>//Service1.cs
  serverPipe = new NamedPipeServerStream(@"testpipe", PipeDirection.InOut, 
     1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
  
  //LoginForm.cs
  clientPipe = new NamedPipeClientStream(@".", @"testpipe", PipeDirection.InOut
      PipeOptions.Asynchronous);
  </code></pre>
  
  <p>I believe this sets up both sides.</p>
  
  <p>To write from one to the other:</p>
  
  <pre><code>//In Service1.cs and Login.cs
  private void pipeWriter()
  {
      StreamWriter write = null;
      write = new StreamWriter(clientPipe);
  
      if (clientPipe.IsConnected)
      {
          write.Write(clientPipe);
          write.Flush();
      }
  }
  </code></pre>
  
  <p>... and to read:</p>
  
  <pre><code>//In Service1.cs and Login.cs
  private void pipeReader()
  {
      try
      {
          while (true)
          {
              using (StreamReader sr = new StreamReader(clientPipe))
              {
                  string message;
  
                  while ((message = sr.ReadLine()) != null)
                  {
                      //read and respond to messages written to stream
                  }
              }
          }
      }
      catch (Exception ex)
      {
  
      }
  }
  </code></pre>
  
  <p>Am I in the right direct? Is there another (better) way to communicate between a Windows service and a form application?</p>