简体   繁体   English

如何监视WInForm应用程序中的弹出错误

[英]How to monitor popup error in WInForm application

I have a very big C# Winform application(contains thousands of exceptions and error popup). 我有一个很大的C#Winform应用程序(包含数千个异常和错误弹出窗口)。 Is there any way that I can monitor this application when it popup error? 弹出错误时,我有什么方法可以监视此应用程序? so that I can disappear it and output some signal after catch it. 这样我就可以消失并在捕获后输出一些信号。

Application.ThreadException (event) that is: Application.ThreadException (事件)即:

Occurs when an untrapped thread exception is thrown. 在引发未捕获的线程异常时发生。

... ...

It uses the ThreadException event to handle UI thread exceptions, and the UnhandledException event to handle non-UI thread exceptions. 它使用ThreadException事件处理UI线程异常,并使用UnhandledException事件处理非UI线程异常。

Read more about this at here . 此处阅读有关此内容的更多信息。

Here is an example code: 这是示例代码:

        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
        }

Caution 警告

Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result. 因为这是一个静态事件,所以在处置应用程序时必须分离事件处理程序,否则会导致内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM