私はException
をグローバルに処理する1つのWPFアプリケーションを開発しています。WPFアプリケーションでエラーフォームApp.xaml.csを表示する方法
私はMSDN
のドキュメントを参照しました。
し、それに応じて私のメインウィンドウ上の私のコード:ここに私のApp.xaml.cs
public App() : base()
{
this.Dispatcher.UnhandledException += Application_DispatcherUnhandledException;
}
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "Exception Sample", MessageBoxButton.OK, MessageBoxImage.Warning);
e.Handled = true;
}
で
private void TestMethod()
{
string s = null;
try
{
s.Trim();
}
catch (Exception ex)
{
MessageBox.Show("A handled exception just occurred: " + ex.Message, "RestartApplication", MessageBoxButton.OK, MessageBoxImage.Warning);
}
s.Trim();
}
私は例外の2 MessageBox
を期待。 Application_DispatcherUnhandledException
が呼び出されていないようです。
しかし、VSはどのように私はApp.xaml.cs
からのエラーおよびshowメッセージボックスを扱うことができる二s.Trim();
にエラーが発生しますか?
dispatcherunhandledexception-does-not-seem-to-work
globally-catch-exceptions-in-a-wpf-application
更新:リアルタイムアプリケーションコード、2番目のメッセージボックスが表示されませんでした。
private void ListProcesses()
{
string s = null;
Process[] localByName = Process.GetProcessesByName("notepad++");
DateTime test = new DateTime();
try
{
s.Trim();
foreach (Process p in localByName)
{
this.Dispatcher.Invoke(() =>
{
if (storevalue != p.MainWindowTitle && !String.IsNullOrEmpty(p.MainWindowTitle))
{
aTimer.Stop();
this.Visibility = Visibility.Visible;
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.Topmost = true;
this.WindowState = System.Windows.WindowState.Maximized;
this.ResizeMode = System.Windows.ResizeMode.NoResize;
storevalue = p.MainWindowTitle;
}
});
}
}
catch (Exception ex)
{
aTimer.Stop();
MessageBoxResult result = MessageBox.Show("A handled exception just occurred: " + ex.Message, "RestartApplication", MessageBoxButton.OK, MessageBoxImage.Warning);
}
s.Trim();
}
'DispatcherUnhandledException'という名前の通り、Not Handed Exceptionです。あなたのコードを見て、私は "処理された例外が発生しました"と "未処理の例外が発生しました"というメッセージが表示されることを期待します... – Pikoh
@Pikoh、[MSDN](https://msdn.microsoft.com/ja) -us/library/system.windows.application.dispatcherunhandledexception.aspx)ドキュメントは状態です*アプリケーションによって例外がスローされたが処理されなかった場合に発生します* –