11

I'm writing a windows phone 7 app. I have "fatal exception" handling code where I know for sure that the app is totally busted and there's no point in continuing. (I'm hoping I never get here...). Since there's nothing more my app can do other than quit I want the user to be able to close the app.

But I noticed there is no System.Environment.Exit() in the Silverlight 4 SDK for Windows Phone 7. Is there another way to quit the app programmatically?

will
  • 3,975
  • 6
  • 33
  • 48
  • 2
    possible duplicate of [Windows Phone 7 close application](http://stackoverflow.com/questions/3659195/windows-phone-7-close-application) – Matt Lacey Oct 22 '10 at 08:12
  • Thought so at first too, but reading on it seems the question is about how to handle unrecoverable problems in an app. The cert reqts advise the exception should be handled and the app left in place for the user to control navigation. – Mick N Oct 26 '10 at 00:09
  • Please read this: http://blog.jerrynixon.com/2011/11/mango-sample-exit-application.html – Jerry Nixon Feb 20 '12 at 19:40

9 Answers9

9

App Cert Reqt:

5.1.2 Application Termination

The application must handle exceptions raised by the .NET Framework and not terminate unexpectedly. During the certification process, the application is monitored for unexpected termination. An application that terminates unexpectedly fails certification. When handling exceptions, an application must provide a user-friendly error message. You may present a message that is relevant to the context of the application. The application must continue to run and remain responsive to user input after the exception is handled. An application that displays generic or unhelpful error messages will fail certification.

I would recommend you provide any information you feel relevant to the user and then leave the navigation of the device to the user to manage in light of this.

Acknowledging known solutions to provide "Exit" buttons, currently I do not see a compelling reason to implement an "exit" from a WP7 application.

The platform is fully capable of managing closure of apps. The more apps don't provide an exit, the quicker users will become accustomed to not thinking about app house keeping, and let the platform manage it.

The user will just navigate their device using start, back, etc.

If the user wants out of the current app to go do something else quickly - easy - they just hit start.

.Exit(), whilst available for xna, really isn't required anymore either. There was a cert requirement during CTP that games had to provide an exit button. This is now gone.

Non game apps never had the need to implement this.

The more this topic's discussed (and it really has been given a good run around the block), the more the indicators to me suggest there is no need to code an exit.

Mick N
  • 14,892
  • 2
  • 35
  • 41
  • I hadn't read that part of the spec, thanks for pointing it out. I guess the thing to do is clearly state that the app can't go on and message that the user quits by hitting the Start hardware button. Thanks – will Oct 25 '10 at 17:23
  • I have an app in the marketplace that throws an Exception to exit and it has never been flagged as an issue. Two caveats: 1) It is clearly labelled as ExitException, so when thrown, it is obvious as to its intention and 2) it is only used in one case, when a user is trying to back out of the app and hits our custom splash screen (which is a page, not an image), which started an animation that otherwise wouldn't let anyone pass. – Joel Shea Mar 29 '11 at 08:55
  • I just had an app rejected from certification because it exits by throwing a cutom ExitException exception. This is despite the help specifically stating the app will exit under these circumstances, an option on the settings screen to disable the behaviour, comments from my beta testers stating the exiting is desirable and useful, and the user being offered the oppourtunity to view the help the first time the app is opened. I have replied to the rejection asking about the likelyhood of receiving a technical exception if I resubmit, but not word yet. – Yort Jan 27 '12 at 00:10
  • I applied for a technical exception for my app, and it was granted. I did have to spell out that the behaviour was desirable, could be turned off in settings and was explained in the help. I also put in a one time prompt just before the app exits the first time, explaining the behaviour and how it could be turned off just to make it clear to the user what was happening. Microsoft must have been happy with that I guess. – Yort Feb 02 '12 at 18:40
4

For Windows Phone 8, simply call App.Current.Terminate();

greensuisse
  • 1,727
  • 16
  • 18
4

Navigate to App.xaml.cs in your solution explorer and add a static method to the App class

public static void Exit()
{
      App.Current.Terminate();
}

so that you can call it anywhere from your application , as below

App.Exit();
3

A "less ugly" (and apparently only) way to exit is outlined here. Yuck.

ctacke
  • 66,480
  • 18
  • 94
  • 155
2

Here's how I do it:

void Exit()
{
    while (NavigationService.BackStack.Any())
        NavigationService.RemoveBackEntry();
    base.OnBackKeyPress(new CancelEventArgs());
}

Unfortunately, this does not work :(

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
0

I suppose you can let your app throw an unhandled exception at which point, Windows Phone will automatically terminate your application.

As for certification requirement, you can ask for exception. This method has always worked for me.

Saif Al Falah
  • 358
  • 5
  • 16
0
App.Current.Terminate();

For Windows Phone 8.1 Silverlight Apps

0

When you are trying to programatically quit a WP7 application, you need keep in mind the application certification requirements. Peter Torr has a blog post that can help with your approach. Paul Jenkins experienced issues with the MahTweets app in the Marketplace recently and he blogged about it here.

HTH, indyfromoz

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
indyfromoz
  • 4,045
  • 26
  • 24
-1
App.Current.MainWindow.Close()
j0k
  • 22,600
  • 28
  • 79
  • 90