title: Communication between Windows service and Windows form application
body: <p>I am working on a project which is a sort of file uploader. In the project, there are certain log files which are created by an application and saved in a particular folder. The content of these files is always text. The contents of these files are uploaded to the server and this task is done by a windows service. The service  reads the files one by one and then transfer their contents to the server.</p>
  
  <p>Earlier this task used to be done by the application itself. But it was separated from application because it is possible that when the application is running the user is offline. So these files will remain pending for upload.</p>
  
  <p>The advantage of using service is obvious that the user doesn't have to worry about upload thing. Whenever the user is connected to internet the service will take care of it.</p>
  
  <p>Now the requirement is that when service uploads the contents of the file and the application via which the files are created is running at that time, the service should send a message to the application that which file's content is being uploaded.</p>
  
  <p>Now the problem is that the service and the application are not communicating. I've spawn seperate threads from both service and application which are using namedpipes for communication. </p>
  
  <p>Here is the code which I've tried.</p>
  
  <p>Service code:</p>
  
  <pre><code>public partial class Service : ServiceBase
  {
  Thread t;
      public Service()
      {
          InitializeComponent();
      }
  
      protected override void OnStart(string[] args)
      {
          File.WriteAllText("D:\\log.txt", "Service started.\n");
          ThreadStart ts = new ThreadStart(Connection);
          t = new Thread(ts);
          t.Start();
      }
  
      void Connection()
      {
          NamedPipeServerStream server = new NamedPipeServerStream("server");
          server.WaitForConnection();
          File.WriteAllText("D:\\log.txt", "Connection established.");
      }
  
      protected override void OnStop()
      {
          if (t.IsAlive)
          {
              t.Abort();
              File.AppendAllText("D:\\log.txt", "Service stopped.");
          }
      }
  }
  </code></pre>
  
  <p>Application code:</p>
  
  <pre><code>public delegate void SendMsg(string s);
  public partial class UI : Form
  {
      Thread t;
  
      public UI()
      {
          InitializeComponent();
      }
  
      private void UI_Load(object sender, EventArgs e)
      {
          lblStatus.Text = "Waiting...";
          ThreadStart ts = new ThreadStart(Connection);
          t = new Thread(ts);
          t.Start();
      }
  
      void DisplayMsg(string s)
      {
          lblStatus.Text = s;
      }
  
      void Connection()
      {
          try
          {
              NamedPipeClientStream client = new NamedPipeClientStream("server");
              SendMsg msg = new SendMsg(DisplayMsg);
              msg("Connecting...");
              client.Connect();
              msg("Connected");
          }
          catch (Exception ex)
          {
              File.WriteAllText("D:\\log.txt", ex.Message);
          }
      }
  }
  </code></pre>
  
  <p>What is wrong with this piece of code? </p>
  
  <p>The same logic works if I use another form application in place of service. </p>
  
  <p>While googling about this, I came to know about Session 0 in Windows Vista onward versions of Windows which is used specially for windows services and is isolated from the other sessions used by logged on users. I'm using Windows 8 in my system. Can this be a problem? Please explain in detail.</p>