By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Problem catching all exceptions in a windows service

P: n/a
Hi all,

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_Unhan dledException);

ServiceBase.Run(servicesToRun);
}

static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in
UnhandledException event handler: " + e.ExceptionObject.ToString());
}
}
Apr 5 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Apr 5, 8:18 am, Simon <s...@nothanks.comwrote:
Hi all,

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_Unhan dledException);

ServiceBase.Run(servicesToRun);
}

static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in
UnhandledException event handler: " + e.ExceptionObject.ToString());
}
That's not the way I do it. The OnStart method must complete within a
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.

Apr 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.