Reputation: 449
So I'm trying to run a service I programmed and for some reason it's giving me this error when try I start it:
Error 1053: the service did not respond to the start or control request in a timely fashion
My code is pretty basic.
static void Main(string[] args)
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Program()
};
ServiceBase.Run(ServicesToRun);
}
catch (Exception ex)
{
string SourceName = "WindowsService.ExceptionLog";
if (!EventLog.SourceExists(SourceName))
{
EventLog.CreateEventSource(SourceName, "Application");
}
EventLog eventLog = new EventLog();
eventLog.Source = SourceName;
string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace);
eventLog.WriteEntry(message, EventLogEntryType.Error);
}
}
public Program()
{
this.ServiceName = "FetchFeed";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
//TODO: place your start code here
repeat: FetchFeed();
Thread.Sleep(3600000);
goto repeat;
}
protected override void OnStop()
{
base.OnStop();
//TODO: clean up any variables and stop any threads
}
private static void FetchFeed()
{
//Some HTTP requests and retrieval.
}
and this is the Installer Class:
[RunInstaller(true)]
public class Service_Installer : Installer
{
public Service_Installer()
{
ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "FetchFeed";
serviceInstaller.StartType = ServiceStartMode.Manual;
//must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "FetchFeed";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
What could be the the reason behind the error? I have checked the FetchFeed() to be working as a standalone application with no Exceptions.
Upvotes: 1
Views: 2480
Reputation: 42453
Using the appopiate timer
public class YourService
{
var tim = new System.Timers.Timer(60 * 60 * 1000); // 1 hour
protected override void OnStart(string[] args)
{
base.OnStart(args);
tim.AutoReset = true;
tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed);
tim.Enabled = true;
// first time run
ThreadPool.QueueUserWorkItem(WaitCallback(FetchFeed));
}
static void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
FetchFeed();
}
}
Upvotes: 1
Reputation: 34013
repeat: FetchFeed();
Thread.Sleep(3600000);
goto repeat;
This is your problem right here. A Windows Service should respond within 30 seconds, so Windows knows is has successfully started.
With this code you are blocking the only thread and the service doesn't respond to anything anymore.
Try doing this on a different thread or another way.
I would suggest another way, because using a goto
is a real no-go :)
Also check out some tutorials and basic documentation on Windows Service to get a better understanding.
Upvotes: 1