c# - Exception in dialog causes MSTest to crash -
i'm researching behaviour in our application , made test consistently makes test engine crash. simplified test code 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 run code dialog pops telling me vstest.executionengine.exe
has stopped working. why happening , can make sure test keeps running when happens?
edit
similar behaviour occurs in 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 code has problems. catching exception in child dialog solve this. i'm trying understand why behaves this.
ok, found out why happens , way solve in application.
exceptions happen in different dispatcher, need handled on dispatcher. exception bubbles stack. stack of sub dialog starts after showdialog called. exceptions happening in dialog, not caught try/catch of calling code. these exceptions need handled.
in question handling exceptions in wpf, found out can handle these exceptions following code:
childwindow.dispatcher.unhandledexception += (o, eventargs) => { // handling code... eventargs.handled = true; };
this how understood it. please correct me if i'm wrong!
Comments
Post a Comment