views:

1025

answers:

5

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.

+2  A: 

It might be stopped unexpectedly if your main thread terminates on exception.

Alex Reitbort
I just removed everything except for creating and starting the timer, with the same results.
SnOrfus
A: 

ThreadPool threads are background threads; they won't keep a process alive. I suspect you need a "proper" thread...

Try: new Thread(SomeCode).Start(); or similar.

Marc Gravell
Tried this - no dice.
SnOrfus
A: 

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?

Jeremy Wilde
though you are correct, the change produced no positive results.
SnOrfus
+1  A: 

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

SnOrfus
A: 

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);
Fritz H
base.OnStart(args) has no implementation in ServiceBase, so the call is not necessary.
Lamar