Reputation: 15958
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();
}
Upvotes: 1
Views: 5817
Reputation: 15958
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();
}
}
}
}
Upvotes: 6
Reputation: 46052
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.
Upvotes: 5
Reputation: 386
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
Upvotes: 1