2

When i need to exit a WPF Application i usually use Application.Current.Shutdown() but sometimes the Application.Currentis NULL,I also tried Environment.Exit(0) but it doesn't work correctly. Is there any other way to exit a WPF Applicationspecialy when Application.Current is NULL?

this is my code:

 public partial class App : Application
 {
    protected override void OnStartup(StartupEventArgs e)
    {
      if (something)
        {
          continue....
        }
      else
        {
        Application.Current.Shutdown()
        }
  }
6
  • From what context you're calling Application.Current? could you post code you're using? Commented Oct 23, 2014 at 8:54
  • I want to close my entire application!and there is just one System.Windows.Application for any AppDomain. Commented Oct 23, 2014 at 8:56
  • 1
    Have you tried this.Close(); ?? Commented Oct 23, 2014 at 8:57
  • @apomene:what do you mean by this? I use this code in App.xaml and the in no This here!!! Commented Oct 23, 2014 at 8:58
  • 3
    the first time I've heard that Application.Current could be null. At least right after the main window was loaded, there is no reason Application.Current could be null. Commented Oct 23, 2014 at 9:03

2 Answers 2

5
System.Diagnostics.Process.GetCurrentProcess().Kill();

Anywhere and anytime this terminates your application immediately.

Sign up to request clarification or add additional context in comments.

Comments

0

This is obviously per design. why would there be an application available when its startup didnt complete yet. Just do this.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (false)
        {
            //
        }
        else
        {
            this.Shutdown();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.