문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top