技术开发 频道

详解.NET多线程异常的处理方法

  更高的要求

  我们能简单的通过全局异常处理事件来记录错误日志;如果保证不中断应用程序,也可以在每个线程入口方法中捕获异常并记录异常日志。有没有办法做到:既能捕获异常且不中断应用程序,又能如全局异常处理事件那样简单捕获异常?

  对于主动创建的新线程,至少可以做到这一点:

public static class ThreadExecutor { public static bool Execute(System.Threading.WaitCallback callback, object state) { try { return System.Threading.ThreadPool.QueueUserWorkItem((data) => { try { callback(data); } catch (exception ex) { // log the exception } }, state); } catch (Exception e) { // log the exception } return false; } }

 

  

0
相关文章