windows service stops and starts immediately, but it shouldn't
Solution 1
The problem turned out top be that the EventLog.WriteEntry was throwing an error because there was no EventSource associated with it. see http://msdn.microsoft.com/en-us/library/xzwc042w.aspx
Solution 2
It might be stopped unexpectedly if your main thread terminates on exception.
Solution 3
The code you posted doesn't make sense to me. Why set an event handler before creating your Timer?
m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
m_Timer = new System.Timers.Timer(90000.0); // 1.5 mins
Shouldn't these two lines be swapped?
Solution 4
As far as I can recall, you must actively report to the service manager that the service has successfully started - otherwise, the OnStart method will return, and if the status change has not been reported, the service manager will assume that the service terminated without actually successfully loading itself.
Reporting your service as having started successfully is done IIRC by the Service base class, so add the following to the bottom of the OnStart
method:
base.OnStart(args);
Nate Cook3
Updated on June 04, 2022Comments
-
Nate Cook3 almost 2 years
I'm creating a windows service and after installing the service, it stops and starts immediately, but it shouldn't be at all. Previously, I was getting errors that the service was not responding to the start command in a timely fashion, so I took the init code out and put it in a thread, and now I am here:
protected override void OnStart(string[] args) { this.EventLog.WriteEntry("ATNotifier Started"); ThreadPool.QueueUserWorkItem(WaitOnEmailsChanged); ThreadPool.QueueUserWorkItem(Init, "IP"); }
The waitonemailschanged thread simply creates a filesystemwatcher to watch to see if the settings file (xml document) gets changed, and loads in the data from that file if that happens. For the time being, this just waits indefinitely (which is the general case, as that will only be changed a few times a year), as no changes are being made to the xml document.
The Init thread does all kinds of things, including creating and starting a System.Timers.Timer object whose Elapsed method is the meat of the service.
I can't understand why it would start and then immediately stop. I should also note that the eventviewer shows no logs from this app.
edit> I tried creating 'proper' threads, with the same results and I've removed everything except the creating and starting of the timer like so:
protected override void OnStart(string[] args) { this.EventLog.WriteEntry("ATNotifier Started"); m_Timer = new System.Timers.Timer(90000.0); // 1.5 mins m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed); m_Timer.Start(); }
and I'm still getting the same message. It's almost as if the OnStart is never being called.