GazTheDestroyer
GazTheDestroyer

Reputation: 21251

Any way to catch exceptions from any spawned thread?

I am developing an app that has plugin support. Plugins may be developed by third parties.

Currently I have a try/catch handler around any calls into the plugins to try and protect my app from third party code. The catch logs the problem but does not crash my app.

My problem is when the third party code spawns threads to do some work. These are not caught by my handler.

Is there any way to catch exceptions from "anything spawned by this function"? Or do I need to just catch them in my global handler?

Upvotes: 2

Views: 172

Answers (2)

Steven
Steven

Reputation: 172666

When an plugin fails and runs within the same AppDomain, it is best to let the application fail itself; thus, stop the application. You won't know in what state the application is when a plugin crashes.

Another option is to run a plugin in its own (child) AppDomain. This way you can unload that app domain and restart that plugin in a newly created AppDomain, without having to restart the application.

You might want to take a look at the Managed Extensibility Framework (MEF). That is especially designed for these scenarios.

Upvotes: 3

nan
nan

Reputation: 20296

Regarding your second wish - catching the exception in a global handler, you can subscirbe the the UnhandledException event in the application domain. It'll catch all exceptions that were not caught in your code.

It is used mainly to log the information of the exception. It won't, however, save your application from terminating.

Upvotes: 2

Related Questions