title: C# Windows service not respond after started
body: <p>I would like to write some string into text file after Windows service started, but it is not respond anything after started. What's wrong in my code?</p>
  
  <p>WindowsService.cs</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Diagnostics;
      using System.Linq;
      using System.ServiceProcess;
      using System.Text;
      using System.IO;
      using System.Threading.Tasks;
      //using System.Threading;
  
      namespace TOU_Transference_Service
      {
          public partial class WindowsService : ServiceBase
          {
              public WindowsService()
              {
                  InitializeComponent();
                  this.ServiceName = "TOUTransference";
                  this.EventLog.Log = "Application";
  
                  // These Flags set whether or not to handle that specific
                  //  type of event. Set to true if you need it, false otherwise.
                  this.CanHandlePowerEvent = true;
                  this.CanHandleSessionChangeEvent = true;
                  this.CanPauseAndContinue = true;
                  this.CanShutdown = true;
                  this.CanStop = true;
              }
              System.Threading.Timer TimerItem;
  
  
              /// &lt;summary&gt;
              /// OnStart(): Put startup code here
              ///  - Start threads, get inital data, etc.
              /// &lt;/summary&gt;
              /// &lt;param name="args"&gt;&lt;/param&gt;
              protected override void OnStart(string[] args)
              {
                  try
                  {
                      ServiceController service = new ServiceController("TOUTransference", ".");
                      if (service.Status == ServiceControllerStatus.Running)
                          WriteLog("Process Started");
                      base.OnStart(args);
                  }
                  catch (Exception err)
                  {
                      throw err;
                  }
              }
  
              /// &lt;summary&gt;
              /// OnStop(): Put your stop code here
              /// - Stop threads, set final data, etc.
              /// &lt;/summary&gt;
              protected override void OnStop()
              {
                  try
                  {
                      ServiceController service = new ServiceController("TOUTransference", ".");
                      if (service.Status == ServiceControllerStatus.Stopped)
                          WriteLog("Process Stopped");
                      base.OnStop();
                  }
                  catch (Exception err)
                  {
                      throw err;
                  }
              }
          private void WriteLog(string text)
          {
              try
              {
                  StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.txt", true);
                  sw.WriteLine(DateTime.Now.ToString() + " : " + text + "\n");
                  sw.Close();
              }
              catch (Exception err)
              {
                  throw err;
              }
          }
      }
  }
  </code></pre>
  
  <p>WindowsServiceInstaller.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Configuration.Install;
  using System.ServiceProcess;
  using System.ComponentModel;
  
  namespace TOU_Transference_Service
  {
      [RunInstaller(true)]
      public class WindowsServiceInstaller : Installer
      {
          /// &lt;summary&gt;
          /// Public Constructor for WindowsServiceInstaller.
          /// - Put all of your Initialization code here.
          /// &lt;/summary&gt;
          public WindowsServiceInstaller()
          {
              ServiceProcessInstaller serviceProcessInstaller =
                                 new ServiceProcessInstaller();
              ServiceInstaller serviceInstaller = new ServiceInstaller();
  
              //# Service Account Information
              serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
              serviceProcessInstaller.Username = null;
              serviceProcessInstaller.Password = null;
  
              //# Service Information
              serviceInstaller.DisplayName = "TOU Transference";
              serviceInstaller.StartType = ServiceStartMode.Automatic;
  
              //# This must be identical to the WindowsService.ServiceBase name
              //# set in the constructor of WindowsService.cs
              serviceInstaller.ServiceName = "TOUTransference";
  
              this.Installers.Add(serviceProcessInstaller);
              this.Installers.Add(serviceInstaller);
          }
  
          private void InitializeComponent()
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Program.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace TOU_Transference_Service
  {
      static class Program
      {
          /// &lt;summary&gt;
          /// The main entry point for the application.
          /// &lt;/summary&gt;
          static void Main()
          {
              ServiceBase[] ServicesToRun;
              ServicesToRun = new ServiceBase[] 
              { 
                  new WindowsService() 
              };
              ServiceBase.Run(ServicesToRun);
          }
      }
  }
  </code></pre>