Created
May 22, 2012 05:50
-
-
Save masaru-b-cl/2766910 to your computer and use it in GitHub Desktop.
エラーが起きたら初期画面再表示なWinFormsアプリケーションサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Windows.Forms; | |
using System.Threading; | |
namespace WindowsFormsApplication1 | |
{ | |
class MyApplicationContext : ApplicationContext | |
{ | |
private Form mainForm; | |
public MyApplicationContext() | |
{ | |
Application.ThreadException += OnThreadException; | |
// メインフォーム表示 | |
mainForm = new LoginForm(); | |
mainForm.FormClosed += OnFormClosed; | |
mainForm.Show(); | |
} | |
private void OnThreadException(object sender, ThreadExceptionEventArgs e) | |
{ | |
var ex = e.Exception; | |
MessageBox.Show("致命的なエラーが発生しました。管理者に連絡してください。"); | |
// TODO : 必要なログ出力 | |
// アプリが落ちないように一度イベントハンドラを解除 | |
mainForm.FormClosed -= OnFormClosed; | |
// メインフォームを閉じる | |
mainForm.Close(); | |
// メインフォームを再表示する | |
mainForm = new LoginForm(); | |
mainForm.FormClosed += OnFormClosed; | |
mainForm.Show(); | |
} | |
private void OnFormClosed(object sender, EventArgs e) | |
{ | |
ExitThread(); | |
} | |
} | |
static class Program | |
{ | |
/// <summary> | |
/// アプリケーションのメイン エントリ ポイントです。 | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Application.Run(new MyApplicationContext()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment