このコードを使用して、WinForm アプリケーションの UnhandledException をキャッチします。
[STAThread]
static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
try
{
Application.Run(new MainForm());
} catch....
そこで、アプリケーションを再起動しようとします。今私の問題は、このような例外をシミュレートすることです。(メインで)試す前に試しました:throw new NullReferenceException("test");
VSがそれをキャッチしました。
ボタンを使用してMainFormコードでも試しました:
private void button1_Click(object sender, EventArgs ev)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestMe), null);
}
protected void TestMe(object state)
{
string s = state.ToString();
}
リリースモードでも、VSはそれをキャッチしました。
- 最後に、アプリケーションを強制的に生成するにはどうすればよい
UnhandleldException
ですか? - でアプリケーションを再起動できます
CurrentDomain_UnhandledException
か? - どうすれば生成でき
ThreadException
ますか?
PS。
VSの外でWindowsの汎用ウィンドウを起動すると
Application MyApplication" でエラーが発生したため、閉じる必要があります...blabla...レポートを送信する/送信しないでください。
私は、しかし、VSは、このメソッドを入力したいです (...Domain_Unhahdled...)
編集: アプリケーションを再起動するとき、次のよう に表示される Windowsクラッシュメッセージを無効にすることは可能 ですか?
コード:
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// Since we can't prevent the app from terminating
// log this to the event log.
Logger.LogMessage(ERROR, errorMsg);
Application.Restart();