0

I'm researching some behaviour in our application and made a test that consistently makes the test engine crash. I simplified the test code into this:

[TestMethod, Timeout(2000)]
public void ShowDialogShouldCleanupAfterException()
{
    var topWindow = new Window();

    topWindow.Loaded += (sender, args) =>
    {
        var childWindow = new Window();
        childWindow.Loaded += (o, eventArgs) =>
        {
            throw new Exception("ChildWindowException");
        };

        childWindow.ShowDialog();
        topWindow.Close();
    };

    topWindow.ShowDialog();
}

Everytime I run this code a dialog pops up telling me vstest.executionengine.exe has stopped working. Why is this happening and can I do something to make sure the test keeps running when this happens?

EDIT

Similar behaviour occurs in a WPF Sample application:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var childWindow = new Window();
        childWindow.Loaded += ChildWindow_Loaded;
        childWindow.Owner = this;
        try
        {
            childWindow.ShowDialog();
        }
        catch (Exception)
        {
            MessageBox.Show("Exception caught");
        }
    }

    private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
...

I understand this code has problems. Catching the exception in the child dialog would solve this. But I'm simply trying to understand why it behaves like this.

Martijn
  • 522
  • 1
  • 6
  • 22
  • It often happen due to recursive calls. Possible duplicate of https://stackoverflow.com/questions/40150167/vstest-crashed-when-running-some-particular-tests – Abhishek K. Upadhyay Jul 25 '17 at 12:18
  • 2
    I wouldn't expect a test runner to have set up the necessary infrastructure for UI to run. – Damien_The_Unbeliever Jul 25 '17 at 12:30
  • @Damien_The_Unbeliever, good suggestion but my other tests featuring UI components just work and I have similar behaviour in a WPF application. That's the reason I'm making these unuasal tests. I edited my question with an example of this in a WPF application. – Martijn Jul 25 '17 at 12:44

1 Answers1

0

Ok, I eventually found out why this happens and a way to solve it in my application.

Exceptions that happen in a different dispatcher, need to be handled on that dispatcher. The exception bubbles up the stack. The stack of a sub dialog starts after the ShowDialog is called. So any exceptions happening in that dialog, will not just be caught by a try/catch of the calling code. These exceptions need to be specifically handled.

In this question about handling exceptions in wpf, I found out that I can handle these exceptions with the following code:

childWindow.Dispatcher.UnhandledException += (o, eventArgs) =>
{
   // handling code...
    eventArgs.Handled = true;
};

This is how I understood it. Please correct me if I'm wrong!

Martijn
  • 522
  • 1
  • 6
  • 22