What happens when a .NET thread throws an exception?

前端 未结 5 2253
南旧
南旧 2020-12-06 00:24

We have an interface IPoller for which we have various implementations. We have a process that will take an IPoller and start it in a separate thread. I\'m trying to come

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 01:15

    You should catch the exception at the method you use at the top of the thread, and do the logging from there.

    An unhandled exception (at the top of a thread) will (in 2.0 onwards) kill your process. Not good.

    i.e. whatever method you pass to Thread.Start (etc) should have a try/catch, and do something useful in the catch (logging, perhaps graceful shutdown, etc).

    To achieve this, you could use:

    • static logging methods
    • captured variables into the delegate (as an anonymous method)
    • expose your method on an instance that already knows about the logger

提交回复
热议问题