views:

164

answers:

1

I have designed a windows service in C#, which runs continiously and spawns three threads. The first thread runs every 15 sec. The second thread runs every min. And the thirs runs only once.

My problem is that this windows service somehow just stops after few days.There is nothing logged in the eventlog on the day of stopping.And it didn't write anything in the logging file.

I want to know what are the various reasons for a windows service to stop abruptly. And this service is not on my DEV box, it is on QA server.

hope it make sense!

+2  A: 

You probably have an unhandled exception on one of your threads- since .NET 2.0, unhandled exceptions will take down the process abruptly like this. Make sure the top of stack for anything you run on a thread has a catch block to handle errors (log, swallow, marshal to another thread, whatever). You can temporarily prevent this behavior (eg, revert to pre-.NET 2.0 behavior) by adding the following to your service's .exe.config file:

<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1" />
  </runtime>
</configuration>

but don't rely on it- it may be removed in a future .NET release (haven't checked 4.0- it may already be gone).

nitzmahone
This is correct. Every thread *must* have a try/catch block that catches any `Exception` and handles the failure gracefully (log to Event Log is a good solution if nothing else is applicable). Otherwise, an unhandled exception in any thread will kill your entire service.
Daniel Pryden
If you're not using failstop logging (ie, log to event log can throw for permissions) make sure your error handling code has a try/catch too, or a log failure could take down the process as well.
nitzmahone
@Daniel Pryden: Will an exception within a thread not be caught by an `AppDomain.UnhandledException` handler? Or is that not a reliable way?
0xA3
AppDomain.UnhandledException is just a notification. It doesn't provide a backstop like it did in 1.1.
nitzmahone