如何在WPF应用程序

时间:2016-10-04 11:08:38

标签: c# wpf error-handling

我正在开发一个WPF应用程序,我将在全球范围内处理Exception

我已经提到了MSDN文件。

因此我的主窗口上的代码:

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();
}

在我的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;
}

在这里,我期待两个MessageBox的异常。并且似乎没有调用Application_DispatcherUnhandledException

但VS在第二个s.Trim();

上发出错误

如何处理错误并显示App.xaml.cs的消息框?

我引用了很多SO的链接,如:dispatcherunhandledexception-does-not-seem-to-work
globally-catch-exceptions-in-a-wpf-application

更新:实时应用程序代码,第二个消息框未显示:

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();
} 

2 个答案:

答案 0 :(得分:1)

呃......我想我知道你发生了什么事。 Dispatcher.UnhandledException事件仅在应用程序运行时有效,而不是从Visual Studio运行时。例如,尝试从Debug文件夹运行它,我认为你会看到预期的行为。

当您从Visual Studio运行应用程序时,VS本身正在处理异常,因此它永远不会被处理,因此永远不会触发Dispatcher.UnhandledException事件。

修改

好的,在研究了您的代码后,我猜您的ListProcesses方法正在Timer中运行。定时器不会将异常传递给调用线程,因此它永远不会工作。如果您使用System.Timers.Timer,它将默默地吞下异常,如果您使用System.Threading.Timer将终止该程序。

因此,在这种情况下,您需要自己处理例外,抱歉:)

答案 1 :(得分:0)

删除你的try / catch块,然后运行“Start Without Debugging( Ctrl + F5 )”。

Application.DispatcherUnhandledException Event 

仅由未处理的异常触发。

这就是我所做的:

public partial class Window12 : Window
{
    public Window12()
    {
        InitializeComponent();

        string id = null;
        id.Trim();
    }
}

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        this.DispatcherUnhandledException += App_DispatcherUnhandledException;
    }

    void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show("Unhandled exception occured > " + e.Exception.ToString());
    }
}