1

I know that I can handle the UnobservedTaskException to prevent any unobserved exceptions from terminating my application when the finalizer runs on the object.

However, I'm not sure where or when I should set up the handler for this event. The XML doc on the event itself within the TaskScheduler class says:

/// This AppDomain-wide event provides a mechanism to prevent exception
/// escalation policy (which, by default, terminates the process) from triggering.
/// Each handler is passed a <see cref="T:System.Threading.Tasks.UnobservedTaskExceptionEventArgs"/> 
/// instance, which may be used to examine the exception and to mark it as observed.

Does this mean I only need one handler in the entire AppDomain? And that all unobserved task exceptions will be handled by that?

Or do I need a handler per assembly / per class, and only unobserved exceptions from tasks within that assembly / class will go to that handler?

Dave New
  • 38,496
  • 59
  • 215
  • 394
Lyall
  • 895
  • 9
  • 20

1 Answers1

2

Per app-domain is all. It's a static event off the Task class which you should just make sure you hook at logical application startup time. So for a console app, just hook it in Main before you start any other work. For ASP.NET apps, just hook it in Global.asax Application_Start. For a Windows service, in OnStart.

Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
  • @Drew Marsh Hi, i did it but dont solved here : http://stackoverflow.com/questions/11831844/unobservedtaskexception-being-throw-but-it-is-handled-by-a-taskscheduler-unobser – newway Aug 07 '12 at 19:19