2011-07-06 109 views
5

我正在检查Windows身份验证用户是否是我的wpf应用程序的有效用户。Application.Current.Shutdown(-1)不关闭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(); 
      } 

     } 
+2

您需要显示'Application.Current.Shutdown()'周围的代码。问题可能与你从哪里调用它有关。 –

+0

我的代码是在App.Xaml - > OnStartup(StartupEventArgs e) – Relativity

+2

你可以发布你的整个App.xaml.cs文件的内容? – NickHeidke

回答

11

使用Environment.Exit()来代替。这将试图优雅地关闭,但如果它不能优雅地关闭,将强行终止线程。

+0

Environment.Exit()的工作..但面临问题DisPose() – Relativity

+1

好的 - 你必须首先显式调用你的对象的Dispose(),因为Environment.Exit将终止你的线程。 Application.Current.Shutdown也不会调用Dispose;没有什么会。 .net中没有自动配置。 –

0

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