Form初期処理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace MyApp.FormApplication
{
static class Program
{
#region アプリケーションのメイン エントリ ポイント
<summary>
</summary>
[STAThread]
static void Main()
{
bool created;
using (Mutex mutex = new Mutex(true, "MyFormAppBase", out created))
{
if (created)
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormApp());
}
else
{
MessageBox.Show("二重起動できません。",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
#region エラーメッセージ表示
<summary>
</summary>
<param name="ex"></param>
<param name="extraMessage"></param>
public static void ShowErrorMessage(Exception ex, string extraMessage)
{
MessageBox.Show(extraMessage + " \n――――――――――――――――――――――――――――――\n\n" +
"エラーが発生しました。開発元にお知らせください\n\n" +
"【エラー内容】\n" + ex.Message + "\n\n" +
"【スタックトレース】\n" + ex.StackTrace,
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
#endregion
#region CurrentDomain_UnhandledExceptionイベント
<summary>
</summary>
<param name="sender"></param>
<param name="e"></param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
ShowErrorMessage(ex, "Application_UnhandledExceptionによる例外通知です。");
}
}
#endregion
#region Application_ThreadExceptionイベント
<summary>
</summary>
<param name="sender"></param>
<param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
ShowErrorMessage(e.Exception, "Application_ThreadExceptionによる例外通知です。");
}
#endregion
}
}