title: Why does my .NET Windows service not start automatically sometimes?
body: <p>I have modified a working Windows service that had always been starting beforehand. After adding the System.Management reference it now sometimes will not start automatically. I get the following error:</p>
  
  <blockquote>
    <p>Service cannot be started.
    System.Runtime.InteropServices.COMException
    (0x80010002): Call was canceled by the
    message filter. (Exception from
    HRESULT: 0x80010002
    (RPC_E_CALL_CANCELED))</p>
  </blockquote>
  
  <p>I found another post here on SO with someone having the same issue. </p>
  
  <p><a href="https://stackoverflow.com/questions/998883/why-wont-my-net-windows-service-start-automatically-after-a-reboot">Why won&#39;t my .Net Windows service start automatically after a reboot?</a></p>
  
  <p>However, the proposed solution was to have the service start after the services it depends on have started. However, when I go to the Dependencies tab for my service, I see:</p>
  
  <p><img src="https://imgur.com/GRcKv.jpg" alt="alt text"></p>
  
  <p>Should I just use the workaround method of putting the thread to sleep, or is there a more proper way of getting this service to start correctly? Is this happening because .NET has not started before my service starts?</p>
  
  <p>Thanks,</p>
  
  <p>Tomek</p>
  
  <p>EDIT: I have added a try-catch statement to catch the exception. Here is the code that I added to the OnStart() method of my service (which is where the exception is being thrown)</p>
  
  <pre><code>        try
          {
              _watcher = new ManagementEventWatcher(query);
              _watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
              _watcher.Start();  
          }
          catch (Exception ex)
          {
              EventLog.WriteEntry("Could not create Sleep/Resume watcher" + ex.Message);
          }
  </code></pre>
  
  <p>The service does start now but without the functionality that I have added. I am new to .NET, but I took the watcher code from a sample I found online, so I am pretty sure it is correct. The Event Log displays the same exception:</p>
  
  <blockquote>
    <p>Could not create Sleep/Resume watcher Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))</p>
  </blockquote>