Why won't my WPF application close properly af

2019-06-25 03:14发布

I am using a 3rd party WinForms Login Dialog in my WPF app, and for some reason this is making my WPF application not close correctly when debugging.

If I exit the winforms login dialog prior to logging in, the application closes correctly, however if I login successfully and launch the WPF window, exiting the window does not shut down the application.

I put a breakpoint in the OnExit method of App and it never gets called. I overwrote the OnClosing method of my main window, and that does get called but even calling Application.Current.ShutDown() doesn't exit the application correctly. I have also tried setting my App's ShutdownMode to OnExplicitShutdown and OnMainWindowClose and neither of those shut it down.

The only hint I have is the following appears in the Debug window, but I have no idea what it's 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 running in Debug mode in Visual Studio. Running the app without debugging closes the app correctly.

Can anyone point me in the right direction as to what could be causing this behavior?

Edit

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

It's used by someething like this:

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
}

2条回答
Melony?
2楼-- · 2019-06-25 03:37

I believe the default shutdown mode is

ShutdownMode = ShutdownMode.OnLastWindowClose

If the application is not shutting down then there are still windows open. Check the Application.Current.Windows collection in the debugger to find the offending window.

So its best, in my opinion, to use

ShutdownMode = ShutdownMode.OnMainWindowClose;

And then make sure to set the following

Application.Current.MainWindow= MyWindow; // Shell etc.
查看更多
何必那么认真
3楼-- · 2019-06-25 03:48

this sounds stupid, but have you tried to call winformsLoginDialog.Close(); in the onClosing method of your main window? I suspect the winforms login dialog still has a thread running, and/or the form may have been hidden instead of closed.

You could try System.Environment.Exit(0)

查看更多
登录 后发表回答