locked
WindowsFormsHost changes exception handling RRS feed

  • Question

  • Using Visual Studio 2008 and .NET 3.5 SP1, adding a WindowsFormsHost control to my WPF application changes its behavior when exceptions are thrown.

     

    I created a fresh WPF application and used the following code in the Window1 class:

    public Window1()
    {
      InitializeComponent();
    
      //this.Content = new System.Windows.Forms.Integration.WindowsFormsHost();
      this.Loaded += new RoutedEventHandler(Window1_Loaded);
    }
    
    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
      throw new NotImplementedException();
    }
    

    Running the program without debugging (ctrl-f5) results in the standard "Sorry, this program had to close" crash dialog, as expected.

    If I uncomment the WindowsFormsHost line, however, and run the program without debugging, no crash dialog appears. The program window is still visible and can be interacted with.

    Why does this happen? And is there any way to get normal behavior back while using a WindowsFormsHost?

    Thursday, October 21, 2010 4:49 PM

Answers

  • Hi Uniquename12345,

    I do reproduce this issue with your code, and actually your can replace WindowsFormsHost with any UIElement and you will get the same result.

    This is because exceptions occur in Window.Loaded event will be handled by WPF internally and it has nothing to do with WindowsFormsHost. However, there is no guarantee that this handled error won't crash the application.

    WPF is supposed to handle this exception and prevent it from crash (unfortunately, as far as I know, there is no way you can tell WPF not to do so in release mode). But under some circumstances such as this, the application will crush anyway.

    Best regards,

    Min 


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, November 1, 2010 2:15 AM
    Wednesday, October 27, 2010 3:39 AM