検索条件
全1件
(1/1ページ)
[STAThread]
static void Main()
{
...
//メインスレッド以外の非ハンドル例外処理
Thread.GetDomain().UnhandledException +=
new UnhandledExceptionEventHandler(Application_UnhandledException);
...
}
public static void Application_UnhandledException(
object sender,
UnhandledExceptionEventArgs e
){
if( e.ExceptionObject != null &&
e.ExceptionObject.GetType() == typeof(ObjectDisposedException)) {
//.NET3.5のSerialPortのバグで、ケーブル挿抜後にハンドル不能の
//ObjectDisposedExceptionが返ることがある。このため、未ハンドル例外処理にて
//ObjectDisposedExceptionについてのみ無視する。
//何もしない
} else {
//何かメッセージを出力
Application.Exit();
}
}
以下のアプリケーション構成ファイルをアプリケーションと同じディレクトリに置く(hoge.exeが実行ファイルなら、hoge.exe.configとなる)。<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <legacyUnhandledExceptionPolicy enabled="1"/> </runtime> </configuration>構成ファイルを上記のように書くと、非ハンドル例外に関しては処理継続となる。釈然としないが、どうもこれが楽な方法らしい。