title: Restart Service C# when error occurs in event
body: <p>I have a service that monitors a folder and performs actions.</p>
  
  <p>This is working perfectly.</p>
  
  <p>However if I deliberately throw an exception within the event method that is called when a file is created, my service stops.  I thought that windows automatically tries to start a service again if it stops, but it looks like this is not the case.</p>
  
  <p>There are several examples for restarting a service which I can use, but I am just wondering if I am missing something obvious here?</p>
  
  <p>It looks as though I will need to put something into my overall try/catch block to restart the service?</p>
  
  <p>This is the method that sets up the folder watcher:</p>
  
  <pre><code>    public void Start()
      {
          var setting = ConfigurationManager.AppSettings["FolderToMonitor"];
          var path = string.IsNullOrEmpty(setting) ? "c:\\temp\\spreadsheets": setting;
  
          var processedPath = path + "\\Processed";
          Directory.CreateDirectory(processedPath);
  
          var errorsPath = path + "\\Errors";
          Directory.CreateDirectory(errorsPath);
  
          _watcher = new FileSystemWatcher
          {
              Path = path,
              NotifyFilter = NotifyFilters.Attributes |
                             NotifyFilters.CreationTime |
                             NotifyFilters.DirectoryName |
                             NotifyFilters.FileName |
                             NotifyFilters.LastAccess |
                             NotifyFilters.LastWrite |
                             NotifyFilters.Security |
                             NotifyFilters.Size,
              Filter = "*.*",
              EnableRaisingEvents = true
          };
  
          _watcher.Created += OnFileCreated;
      }
  </code></pre>
  
  <p>When a file is created this event is called</p>
  
  <pre><code>   private void OnFileCreated(object sender, FileSystemEventArgs e)
     {
       while (!IsFileReady(e.FullPath))
       {
       }
  
       Process(e.FullPath);
    }
  </code></pre>
  
  <p>Here I am now deliberately throwing an exception:</p>
  
  <pre><code>    public virtual void Process(string filename)
      {
          var path = Path.GetDirectoryName(filename);
          var processedPath = path + "\\Processed";            
          var errorsPath = path + "\\Errors";
  
          var filenameOnly = Path.GetFileName(filename);
  
          try
          {
              ReadFile(filename);
  
              if (filename.Contains("hello"))
              {
                  throw new Exception("TEST ERRROR");
              }
          }
          catch (Exception e)
          {
              Log.Error(e, "Error processing file");
              File.Move(filename ?? throw new InvalidOperationException(), $"{errorsPath}\\{filenameOnly}");
  
              throw;
          }
  
          Log.Information($"File processed {filename}.  Moved to processed folder");
          File.Move(filename ?? throw new InvalidOperationException(), $"{processedPath}\\{filenameOnly}");
      }
  </code></pre>
  
  <p>If I try to restart the service myself using the code below</p>
  
  <pre><code>var service = ServiceController.GetServices("My Service Name").FirstOrDefault();
              if (service?.Status == ServiceControllerStatus.Stopped)
              {
                  service.Start();
              }
  </code></pre>
  
  <p>I get the error </p>
  
  <pre><code>'Cannot open Service Control Manager on computer 'My Service Name'. This operation might require other privileges.'
  </code></pre>
  
  <p>I have double and tripled checked - my service name is correct.  I am also running Visual Studio in Admin mode.</p>
  
  <p>The service is being run as the <strong>Local System</strong> user.</p>