【问题标题】:Application.Current.Shutdown(-1) not closing WPF appApplication.Current.Shutdown(-1) 不关闭 WPF 应用程序
【发布时间】:2011-07-06 19:31:02
【问题描述】:

我正在检查 Windows 身份验证用户是否是我的 wpf 应用程序的有效用户。

如果没有,我需要关闭应用程序;但即使在执行 Application.Current.Shutdown(-1) 之后,应用程序也会继续愉快地执行。

下面的链接说我需要删除我的 StartUpURI;但我的 app.xaml 中没有那个标签。 -> Shutting down a WPF application from App.xaml.cs

编辑:- 我在 APP.XAML.CS 中有这段代码 ->

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            this.exceptionPolicy = ConfigurationManager.AppSettings.Get("ExceptionPolicy");
            this.displayErrorDetails = true;
            this.container = new UnityContainer();

            // Register services and types in Unity
            RegisterServices();

            // Check user
            if (!IsValidUser())
            {
                //Application.Current.Shutdown(); 
                App.Current.Shutdown();
            }

        }

【问题讨论】:

  • 你需要显示Application.Current.Shutdown()周围的代码。问题可能与您从哪里调用它有关。
  • 我的代码在 App.Xaml -> OnStartup(StartupEventArgs e)
  • 您可以发布您的整个 App.xaml.cs 文件内容吗?
  • 您正在寻找哪个特定代码?...因为复制整个代码会搞砸事情。
  • 现在请把整个OnStartup方法。只需在每行前面放置 4 个空格以使其格式正确。如果这还不够显示,其中的代码可能会告诉我们还需要查看什么。

标签: wpf application-shutdown


【解决方案1】:

请改用Environment.Exit()。这将尝试优雅地关闭,但如果不能优雅地关闭,则会粗鲁地关闭——强制终止线程。

【讨论】:

  • 嗯,是的——您必须首先在您的对象上显式调用 Dispose(),因为 Environment.Exit 将终止您的线程。 Application.Current.Shutdown 也不会调用 Dispose;什么都不会。 .net 中没有自动处置。
【解决方案2】:

我从来没有运气在启动时关闭某些东西。我建议启动一个新线程,在短暂延迟后,使用示例中的类似代码关闭应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多