Пример #1
0
        // Task线程内未捕获异常处理事件
        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            Exception ex  = e.Exception;
            string    msg = string.Format($"{ex.Message}\n\n{ ex.StackTrace}");

            MessageBox.Show(msg, " Task异常");
            MyUtil.SaveAppLogFile("ErrorLog", msg);
        }
Пример #2
0
        // UI线程未捕获异常处理事件(UI主线程)
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Exception ex  = e.Exception;
            string    msg = string.Format($"{ex.Message}\n\n{ ex.StackTrace}"); // 异常信息 和 调用堆栈信息

            MessageBox.Show(msg, " UI线程异常");
            MyUtil.SaveAppLogFile("ErrorLog", msg);
        }
Пример #3
0
        // 非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
        // 如果UI线程异常DispatcherUnhandledException未注册,则如果发生了UI线程未处理异常也会触发此异常事件
        // 此机制的异常捕获后应用程序会直接终止。没有像DispatcherUnhandledException事件中的Handler=true的处理方式,可以通过比如Dispatcher.Invoke将子线程异常丢在UI主线程异常处理机制中处理
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;

            if (ex != null)
            {
                string msg = string.Format($"{ex.Message}\n\n{ ex.StackTrace}");    // 异常信息 和 调用堆栈信息
                MessageBox.Show(msg, " 非UI线程异常");
                MyUtil.SaveAppLogFile("ErrorLog", msg);
            }
        }