Application.CurrentDomain.UnhandledException にバインドされたイベント ハンドラーを持つ .NET プログラムがあります。デバッグを使用してプログラムを実行している場合、ハンドルされない例外がスローされると、このイベントが発生します。ただし、デバッグなしで実行すると、イベントは発生しません。
私の問題は何ですか?
ありがとう、アンドリュー
Application.CurrentDomain.UnhandledException にバインドされたイベント ハンドラーを持つ .NET プログラムがあります。デバッグを使用してプログラムを実行している場合、ハンドルされない例外がスローされると、このイベントが発生します。ただし、デバッグなしで実行すると、イベントは発生しません。
私の問題は何ですか?
ありがとう、アンドリュー
を使用して正しい例外処理モードを設定していないと思いますApplication.SetUnhandledExceptionMode()
- に設定するだけUnhandledExceptionMode.ThrowException
です。
アップデート
小さなテスト アプリケーションを作成したところ、予想外に動作するものは何も見つかりませんでした。このテスト コードでエラーを再現してみてください。
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication
{
public static class Program
{
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.Run(new TestForm());
throw new Exception("Main");
}
static void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "Application.ThreadException");
}
static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
}
}
public class TestForm : Form
{
public TestForm()
{
this.Text = "Test Application";
this.ClientSize = new Size(200, 60);
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
this.StartPosition = FormStartPosition.CenterScreen;
Button btnThrowException = new Button();
btnThrowException.Text = "Throw";
btnThrowException.Location = new Point(0, 0);
btnThrowException.Size = new Size(200, 30);
btnThrowException.Click += (s, e) => { throw new Exception("Throw"); };
Button btnThrowExceptionOnOtherThread = new Button();
btnThrowExceptionOnOtherThread.Text = "Throw on other thread";
btnThrowExceptionOnOtherThread.Location = new Point(0, 30);
btnThrowExceptionOnOtherThread.Size = new Size(200, 30);
btnThrowExceptionOnOtherThread.Click += (s, e) => new Thread(() => { throw new Exception("Other thread"); }).Start();
this.Controls.Add(btnThrowException);
this.Controls.Add(btnThrowExceptionOnOtherThread);
}
}
}
アプリケーションの別のスレッド内で例外がスローされる可能性があります。スレッド内で未処理の例外が発生したときに、アプリケーションが単に「停止」する (つまり、1 秒停止し、もう 1 秒停止する) という問題が発生しました。その場合、未処理の例外ハンドラーでさえトリガーされませんでした。