1
votes

I followed the instructions in the marked answer here to create a service. The service gets installed correctly. When I start the service after a while it throws a message "Windows could not start the xxx service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion. "

After I click ok, its status stays at "Starting" for ever. When I checked the application and system logs, there were no errors.

When I check the SQL trace, the service is actually running correctly and doing what its supposed to do. So why does its status stay at "Starting" ?

Update: This is the code in OnStart method

 protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();
            loader.StartProcess();
        }

Update 2:

based on WiktorZychla's comment I did this and it worked :)

protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();

            ThreadStart threadDelegate = new ThreadStart(loader.StartProcess);
            Thread newThread = new Thread(threadDelegate);
            newThread.Start();

        }
3
What is in your on start? Are you spinning? Locking? Waiting? In other words: does the OnStart method ever finish?rene
If the loader.StartProcess never returns, you have what you see. In OnStart you should rather start a new thread and the method should return just after.Wiktor Zychla
@WiktorZychla: If I start a new thread on OnStart, do I need to do a thread.abort on OnStop?developer747
Yes, you do need to do that (sorry for the verbose comment but comments shorter than 15 characters are disallowed)Wiktor Zychla

3 Answers

6
votes

Based on WiktorZychla's comment this is what I did

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using IndexLoader;
using System.Threading;

namespace myNameSpace
{
    public partial class LoaderService : ServiceBase
    {
        Thread newThread;
        public LoaderService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();

            ThreadStart threadDelegate = new ThreadStart(loader.StartProcess);
            newThread = new Thread(threadDelegate);
            newThread.Start();

        }

        protected override void OnStop()
        {
            if ((newThread != null) && (newThread.IsAlive))
            {


                Thread.Sleep(5000);
                newThread.Abort();

            }
        }
    }
}
5
votes

It would be instructive to know exactly what is in your OnStart() method. The OnStart() method is a callback from the OS that is used to start your service, but it must return within 30 seconds or so (I remember reading that somewhere). Otherwise, the OS gives the message you're seeing. In short, limit the OnStart() method to getting things initialized, and defer the actual work your service is to perform to a thread of some kind.

1
votes

In your Program.cs file in the Main() function make sure that you have:

ServiceBase.Run(new ServiceClassHere());

I've been guilty many times when creating a windows form app of keeping

Application.Run(new Class()); in my Main() function