2016-10-04 66 views
1

我开发一个WPF应用程序中,我已经处理Exception全球显示错误形式App.xaml.cs。如何在WPF应用程序

对于我已经指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; 
} 

在这里,我期待2 MessageBox一个例外。并且好像没有调用Application_DispatcherUnhandledException

但我怎么能处理来自App.xaml.cs错误,并显示消息框VS给出了第二s.Trim();

错误?

我已经指的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(); 
} 
+0

'DispatcherUnhandledException'作为它的名称说,是为了处理异常。看到你的代码,我会期待一个“刚刚发生的处理的异常”,后面跟着“刚刚发生的未处理的异常”... – Pikoh

+0

@Pikoh,我有[MSDN](https://msdn.microsoft.com/zh-cn/ -us/library/system.windows.application.dispatcherunhandledexception.aspx)文件状态*当应用程序抛出异常但未处理时发生* –

回答

1

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

当您从Visual Studio中您的应用程序,VS本身处理异常所以它永远不会是未处理的,因此从来没有烧制Dispatcher.UnhandledException事件。

编辑

好,学习你的代码后,我想你ListProcesses方法在Timer运行。定时器不会将异常传递给调用线程,所以它永远不会工作。如果您正在使用System.Timers.Timer它将默默吞下异常,并且如果使用System.Threading.Timer将终止程序。

因此,在这种情况下,你需要采取例外好好照顾自己,对不起:)

+0

好的,让我试试。 –

+0

不,它只是显示第一个消息框! –

+0

我刚刚试过你的代码,工作得很好。你确定你正在运行exe文件@HinaKhuman? – Pikoh

0

删除您try/catch块,并运行“开始不调试(按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()); 
    } 
} 
+0

它不工作没有错误。 –

+0

@HinaKhuman如果你删除try/catch块,它肯定会奏效。我再次检查它。 – AnjumSKhan

+0

对不起,实际上我删除了try catch块并运行我的'exe'文件,但没有显示给我一个消息框。 –

相关问题