title: windows service best practice
body: <p>For some reason(e.g. I want it to run automatically at system start-up and last forever), I decide to develop my program as a windows service application. Currently, my implementation is as follows:</p>
  
  <ol>
  <li>Implement the main business logic as a class library.</li>
  <li>Implement a windows console application as client program which will construct the business object and periodically call the business logic component.</li>
  <li><p>Develop the windows service application to start and stop the console application.</p>
  
  <p>3.1 Start the process in the OnStart method.</p></li>
  </ol>
  
   <pre class="lang-cs prettyprint-override"><code>    djsProcessStartInfo = new ProcessStartInfo()
          {
                  UseShellExecute = false,
                  WorkingDirectory = rootDir + depolyDate,
                  Arguments = args,
                  FileName = rootDir + depolyDate + @"\" + appName                
          };
          try
          {
              djsProcessToRun = Process.Start(djsProcessStartInfo);
          }
          catch(Exception ex)
          {
               ///
          }
  </code></pre>
  
  <p>3.2 Stop the process within OnStop method.</p>
  
  <pre class="lang-cs prettyprint-override"><code>        if (djsProcessToRun != null)
          {
              try
              {
                  djsProcessToRun.Kill();
              }
              catch (Exception ex)
              {
                  ///...
              }
          }
          else
          {
              ///...
          }
  </code></pre>
  
  <p>Is this the right way to develop the windows service application?
  I happened to encounter the problem when the process djsProcessToRun failed to run or sometimes I cannot stop it. 
  Is there any best practice there I should follow?(e.g. how to handle exception, how to separate the function between windows service and the target windows application)</p>