Chase
Chase

Reputation: 944

Unable to Manually Start C# Windows Service

I have created a Windows Service project in Visual Studio Community 2015 on my Windows 10 laptop (upgraded from Windows 7 and fully updated), but I have been unable to successfully start my service manually, and I am unable to force kill the service once it has begun to start. When I attempt to start my service via the Services Console (services.msc), I get the following behavior:

  1. A "Service Control" dialog pops up with a progress bar.
  2. The progress bar fills slowly over the course of 90 seconds.
  3. When the progress bar is full, I receive Error 1053.
  4. The service's state changes to and remains infinitely at "Starting" (Start Pending).
  5. The service can not be stopped by any means besides a forced reboot.

I've come to understand that Error 1053 is the result of a service that is unable to start after 30 seconds. However, there is no possibility of my service taking longer than 30 seconds to start. My OnStart, OnStop, and OnTimer methods are currently as follows:

protected override void OnStart(string[] args)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 10000;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
    timer.Start();
}

protected override void OnStop()
{
}

public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
}

And my Main method looks like this:

static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
        new MyNewService()
    };
    ServiceBase.Run(ServicesToRun);
}

Strangely, if my service's startup method is set to "Automatic" then the service will indeed start on its own the next time Windows restarts. When it is started automatically, I am able to manually stop the service (this is the only time it will stop manually). However, after successfully stopping it manually, I still cannot re-start it manually. Attempting to do so produces the exact same behavior as described earlier, resulting again in Error 1053.

My service installs without error (via installutil), and I can uninstall without error. However, if I attempt to uninstall my service (via installutil /u) while the service is stuck in "Starting" (Start Pending) mode, the service does not stop, remains as "Starting", and the startup mode switches to "Disabled". Only upon restarting is the service completely removed.

My question: Why does my service start automatically on Windows startup, but not by any other manual method? (I need it to start automatically, but also be able to manually start and stop it.)

Thank you for your time and expertise.

Upvotes: 2

Views: 2380

Answers (2)

Chase
Chase

Reputation: 944

The issue was Avast anti-virus stopping the service silently. Annoyingly, Avast does not report the service as a virus or a threat of any kind and makes no log of the blocked service. I was able to successfully start and stop my service manually once I had disabled Avast. As a semi-permanent solution, I have added the path to my service to Avast's exceptions list by going to Settings -> Active Protection -> Customize (File System Shield) -> Exclusions, and clicking "Add."

To answer my original question, my service was able to start automatically on Windows startup because it was able to start before Avast was loaded. This led to the strange behavior I observed where I was able to stop it after it had started automatically, but not restart it manually since Avast was now running by that point.

Upvotes: 2

BWA
BWA

Reputation: 5764

You shoud cleanup service in method OnStop. You create a timer and never stop it.

private System.Timers.Timer timer;

protected override void OnStart(string[] args)
{
    timer = new System.Timers.Timer();
    timer.Interval = 10000;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
    timer.Start();
}

protected override void OnStop()
{
    timer.Stop();
    timer = null;
}

Upvotes: 1

Related Questions