2020-08-17

C# WinForm捕获全局异常

根据网上的资料稍微修改了一下,原理很简单。static void Main() { GlobalExceptionCapture(() => { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }); }

根据网上的资料稍微修改了一下,原理很简单,代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using System.Text;namespace WindowsFormsApplication2{ static class Program {  /// <summary>  /// 应用程序的主入口点。  /// </summary>  [STAThread]  static void Main()  {   GlobalExceptionCapture(() =>    {    Application.EnableVisualStyles();    Application.SetCompatibleTextRenderingDefault(false);    Application.Run(new Form1());   });     }  static void GlobalExceptionCapture(Action mainContent)   {   try   {    //设置应用程序处理异常方式:ThreadException处理    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);    //处理UI线程异常    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);    //处理非UI线程异常    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);    #region 应用程序Main()内容包装的委托    mainContent();    

No comments:

Post a Comment