簡體   English   中英

Windows Forms 未處理的異常崩潰

[英]Windows Forms Unhandled Exception Crash

我有適合我的 winforms 應用程序的代碼來處理未處理的異常。 然而,我的應用程序仍然崩潰。

在這個階段,我確實不明白為什么會出現這種行為。 我會很感激你的幫助。

這是我的代碼:

 [STAThread]
    private static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        bool result;
        var mutex = new System.Threading.Mutex(true, "MyApplication", out result);
        if (!result)
        {
            MessageBox.Show("Another instance is already running.");
            return;
        }
        Application.ThreadException += ApplicationThreadException;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
        Memory.frmMain = new MainForm();
        Application.Run(new MyApplicationContext());
        GC.KeepAlive(mutex);
    }
    public static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show(((Exception)e.ExceptionObject).Message);
        ((Exception)e.ExceptionObject).AddLog();
        Memory.processtranslations.IsProcessing.Enabled = true;
    }
    public static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message);
        e.Exception.AddLog();
        Memory.processtranslations.IsProcessing.Enabled = true;
    }

嘗試將[HandleProcessCorruptedStateExceptions]屬性添加到您的 Main 方法中。 更重要的是,讓你的主循環進入 try..catch:

[HandleProcessCorruptedStateExceptions]
static void Main(string[] args)
{
 //other code
  
  try
  {
    Application.Run(new MyApplicationContext());
  }catch(Exception)
  {
    //...
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM