title: C# Windows Service not running correctly
body: <p>I've written a Windows Service (using Visual Studio 2010 Premium, C#, .NET 4) to monitor a folder. All it does is detect when a change is made in the folder (a file/folder added, something deleted, etc) and writes the detections to a text file. Not an Event Log, just a normal .txt file.</p>
  
  <p>Now, it installs fine (I presume), and it shows up in Computer Management and allows me to run it. It supposed to write "Monitoring started" to the file - but it doesn't. Then, when I try to stop the service it gives me this error:</p>
  
  <p><em>"Windows could not stop the FileMonitoring service on Local Computer. The service did not return an error. This could be an internal Windows error or an internal service error. If the problem persists, contact your system administrator."</em></p>
  
  <p>And doesn't stop. Then, when I attempt to stop it again, I get this error:</p>
  
  <p><em>"Windows could not stop the FileMonitoring service on Local Computer. Error 1061: The service cannot accept control messages at this time."</em></p>
  
  <p>And then it stops.</p>
  
  <p>Does anyone have any idea how to resolve this issue? Before this service, I just made a simple service that wrote to a .txt file "Started" when the service started and "Stopped" when the service stopped, and it worked perfectly. All that's added into this is the used of <code>FileSystemWatcher</code>.</p>
  
  <p>Any help would be much appreciated, and I'll give any info/code snippets you need, just ask.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's some code for you:</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      watcher = new FileSystemWatcher();
      watcher.Path = @"C:\temp\services\Watched";
  
      watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
      watcher.Created += new FileSystemEventHandler(LogFileSystemChanges);
      watcher.Deleted += new FileSystemEventHandler(LogFileSystemChanges);
      watcher.Renamed += new RenamedEventHandler(LogFileSystemRenaming);
      watcher.Error += new ErrorEventHandler(LogBufferError);
      watcher.EnableRaisingEvents = true;
  
      LogEntry("Monitoring Started.");
  }
  
  protected override void OnStop()
  {
      watcher.EnableRaisingEvents = false;
      watcher.Dispose();
  
      LogEntry("Monitoring Stopped.");
  }
  
  void LogEntry(string message)
  {
      counter++;
  
      FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
      StreamWriter sw = new StreamWriter(fs);
  
      sw.WriteLine(counter + ". " + message);
  
      fs.Close();
      sw.Close();
  }
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Okay, someone noted the fact I had not closed my Streams. Silly, silly me. Now, I ran the service after that, and didn't get the same error, I got this one:</p>
  
  <p><em>"The FileMonitoring service on Local Computer started and then stopped. Some services stop automatically is they are not in use by other services or programs."</em></p>
  
  <p>This seems a lot simpler but I'm still at a loss. Any ideas?</p>
  
  <p>Thanks so much!</p>