cost
cost

Reputation: 4480

How to deal with exceptions on separate threads in an external dll?

I'm loading in a dll that spawns a few threads within itself. Every now and then the program crashes with an unhandled exception. I can't wrap all my library calls in try/catch statements because the exceptions are being thrown on separate threads, and I don't have access to the library's source to debug/fix the bad error handling. I'd like the program to keep doing other things when these exceptions happen, is there a graceful way to handle these exceptions? Or is the only thing I can do to do a global catch all exceptions method?

Upvotes: 1

Views: 545

Answers (2)

Yahia
Yahia

Reputation: 70369

I would recommend to implement a separate process (EXE) which your application launches and which in turn loads the DLL.

This allows you to kill/restart the process whenever need be...

I see several options on how to communicate - for example:

  • you could use COM (if you implement it as an out-of-process COM server)
  • you could use shared memory (very high performance, see this for a walkthrough and this for a .NET 2 wrapper)

IF the method must be compatible with several Windows versions THEN I would refrain from using anything "networky" for the IPC (since some come with a desktop firewall).

Upvotes: 1

Immortal Blue
Immortal Blue

Reputation: 1700

If you load the DLL into a separate appdomain, you should be able to isolate exceptions generated with a AppDomain.UnhandledException, but, be aware that this is not fool proof and there are certain situations where it will still take your process out too and there is nothing you can do about it (stack overflow, out of memory etc).

The best you can do in that case is load them in a separate process completely with some kind of dll communication harness and using some form of remoting to talk to that process.

Upvotes: 1

Related Questions