全球异常处理程序的窗口服务吗?异常、窗口、程序、全球

由网友(許丅的承诺,欠丅的债)分享简介:有没有在全球范围内处理异常的Windows服务的方式?类似的事情在Windows下窗体应用程序:Application.ThreadException + =新ThreadExceptionEventHandler(新ThreadExceptionHandler()ApplicationThreadException。...

有没有在全球范围内处理异常的Windows服务的方式?类似的事情在Windows下窗体应用程序:

  Application.ThreadException + =新ThreadExceptionEventHandler(新ThreadExceptionHandler()ApplicationThreadException。);
 

解决方案

下面是一些pretty的稳健code,我们提醒人们,当他们正在实施的 http://exceptioneer.com 在他们的Windows应用程序。

 命名空间YourNamespace
{
    静态类节目
    {

        [STAThread]
        静态无效的主要()
        {
            AppDomain.CurrentDomain.UnhandledException + = CurrentDomain_UnhandledException;
            Application.ThreadException + =新System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(假);
            Application.Run(新Form1中());
        }

        静态无效Application_ThreadException(对象发件人,System.Threading.ThreadExceptionEventArgs E)
        {
            HandleException(e.Exception);
        }

        静态无效CurrentDomain_UnhandledException(对象发件人,UnhandledExceptionEventArgs E)
        {
            HandleException((异常)e.ExceptionObject);
        }

        静态无效HandleException(例外五)
        {
            //这里处理的异常
        }

    }
}
 
Java中的异常以及异常处理

谢谢

菲尔。

Is there a way to globally handle exceptions for a Windows Service? Something similar to the following in Windows Forms applications:

Application.ThreadException += new ThreadExceptionEventHandler(new ThreadExceptionHandler().ApplicationThreadException);

解决方案

Here is some pretty robust code we advise people to use when they're implementing http://exceptioneer.com in their Windows Applications.

namespace YourNamespace
{
    static class Program
    {

        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
            //Handle your Exception here
        }

    }
}

Thanks,

Phil.

阅读全文

相关推荐

最新文章