|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem catching all exceptions in a windows serviceI'm having a problem creating a global exception handler in a windows service that I'm making. Another poster suggested that the way to do this was to use the CurrentDomain.UnhandledException event to catch and deal with any exception that crops up. The problem is, when I try and deliberately throw an exception in my services OnStart event handler, nothing seems to happen. By that I mean, the CurrentDomain_UnhandledException handler that I've created never seems to be called. Can anyone advise me if I'm doing this correctly? I've included the code below. I just set up the event handler in the main method... Thanks to anyone who can offer any help Kindest Regards Simon static class Program { static void Main() { ServiceBase[] servicesToRun; servicesToRun = new ServiceBase[] { new REMUSSchedulerService() }; AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); ServiceBase.Run(servicesToRun); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in UnhandledException event handler: " + e.ExceptionObject.ToString()); } } On Apr 5, 8:18 am, Simon <s***@nothanks.com> wrote:
Show quote > Hi all, That's not the way I do it. The OnStart method must complete within a> > I'm having a problem creating a global exception handler in a windows > service that I'm making. Another poster suggested that the way to do > this was to use the CurrentDomain.UnhandledException event to catch and > deal with any exception that crops up. The problem is, when I try and > deliberately throw an exception in my services OnStart event handler, > nothing seems to happen. By that I mean, the > CurrentDomain_UnhandledException handler that I've created never seems > to be called. > > Can anyone advise me if I'm doing this correctly? I've included the code > below. I just set up the event handler in the main method... > > Thanks to anyone who can offer any help > > Kindest Regards > > Simon > > static class Program { > > static void Main() { > ServiceBase[] servicesToRun; > servicesToRun = new ServiceBase[] { new REMUSSchedulerService() }; > > AppDomain.CurrentDomain.UnhandledException += new > UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); > > ServiceBase.Run(servicesToRun); > } > > static void CurrentDomain_UnhandledException(object sender, > UnhandledExceptionEventArgs e) { > TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in > UnhandledException event handler: " + e.ExceptionObject.ToString()); > } > > > certain amount of time (I think 30 seconds) or the SCM will think something is wrong. It must not throw exceptions or the service will not start. Now, typically, the OnStart method just gets something going asynchronously like a timer or thread and returns immediately. The timer callback/event or thread is the place I put a "global" exception handler. |
|||||||||||||||||||||||