Why won't my WPF application close after displaying the WinForms dialog box?

I use a third-party WinForms input dialog in my WPF application, and for some reason this leads to my WPF application not closing correctly when debugging.

If I exit the winforms login dialog before logging in, the application will close correctly, however, if I log in and start the WPF window, exiting the window does not exit the application.

I set a breakpoint in the OnExit App method and never OnExit called. I OnClosing method of my main window and calls the call, but even the call to Application.Current.ShutDown() does not exit the application correctly. I also tried OnExplicitShutdown ShutdownMode application to OnExplicitShutdown and OnMainWindowClose , and none of them closed it.

The only hint I have is that the next window appears in the Debug window, but I have no idea what it is trying to tell me.

 The thread 'vshost.RunParkingWindow' (0xf74) has exited with code 0 (0x0). The thread <No Name> (0x1b58) has exited with code 0 (0x0). 

This behavior only occurs when working in debug mode in Visual Studio. Running the application without debugging closes the application correctly.

Can someone point me in the right direction as to what might cause this behavior?

Edit

The WinForms login dialog is not a traditional login dialog using the Show() or Close() method, it is a static class that only has the Login() and Logout() method.

Something like this is used:

 if (CompanyNamespace.ApplicationName.Login()) { var shell = new ShellView(); var context = new ShellViewModel(); shell.DataContext = context; shell.Show(); // When the Shell Window gets closed, the debugger doesn't stop } else { Application.Current.Shutdown(); // Works fine } 
+4
source share
2 answers

This sounds silly, but you tried to call winformsLoginDialog.Close (); in the onClosing method of your main window? I suspect that the login dialog in winforms still has a thread and / or the form may be hidden rather than closed.

You can try System.Environment.Exit(0)

+3
source

I believe the default break mode

 ShutdownMode = ShutdownMode.OnLastWindowClose 

If the application does not close, windows still open. Check the Application.Current.Windows collection in the debugger to find an abusive window.

So what is the best, in my opinion, to use

 ShutdownMode = ShutdownMode.OnMainWindowClose; 

And then make sure the following are installed

 Application.Current.MainWindow= MyWindow; // Shell etc. 
+1
source

All Articles