为什么我的 WPF 应用程序在显示 WinForms 对话框后无法正确关闭? [英] Why won't my WPF application close properly after it displays a WinForms dialog?

查看:32
本文介绍了为什么我的 WPF 应用程序在显示 WinForms 对话框后无法正确关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 WPF 应用程序中使用了 3rd 方 WinForms 登录对话框,由于某种原因,这导致我的 WPF 应用程序在调试时无法正确关闭.

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.

如果我在登录前退出 winforms 登录对话框,应用程序会正确关闭,但是如果我成功登录并启动 WPF 窗口,退出窗口不会关闭应用程序.

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.

我在 AppOnExit 方法中放置了一个断点,它永远不会被调用.我覆盖了主窗口的 OnClosing 方法,它确实被调用了,但即使调用 Application.Current.ShutDown() 也不能正确退出应用程序.我还尝试将我的应用程序的 ShutdownMode 设置为 OnExplicitShutdownOnMainWindowClose 并且它们都没有关闭它.

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).

此行为仅在 Visual Studio 中以调试模式运行时发生.运行应用而不进行调试会正确关闭应用.

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?

编辑

WinForms 登录对话框不是带有 Show()Close() 方法的传统登录对话框,它是一个只有 Login 的静态类()Logout() 方法.

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.

它被这样的东西使用:

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
}

推荐答案

这听起来很愚蠢,但是您是否尝试过调用 winformsLoginDialog.Close();在主窗口的 onClosing 方法中?我怀疑 winforms 登录对话框仍然有一个线程在运行,和/或表单可能已隐藏而不是关闭.

你可以试试System.Environment.Exit(0)

这篇关于为什么我的 WPF 应用程序在显示 WinForms 对话框后无法正确关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆