2011-07-06 152 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

我從來沒有運氣關閉啓動的東西。我會建議開始一個新的線程,在短暫的延遲後,使用您在示例中使用的類似代碼關閉應用程序。