Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Talk:How to terminate a Windows Phone app

From Wiki
Jump to: navigation, search


SB Dev -

Nice article, I have run into situations where it was necessary to use this. In the end you mention that people should make sure to save important data. You should perhaps mention that they can't rely on the regular application lifecycle events for this because after Terminate the App terminates immediately without firing any of the regular events.

SB Dev (talk) 07:42, 4 September 2013 (EEST)

Influencer - Broadly discussed


thanks for the article, but...

App Termination for WP8 (and WP7) has already been broadly discussed,in blogs How to Terminate the WP8 Application Programmatically using C#? and discussion threads, even on Nokia Discussions Closing Windows Phone 8 apps programatically . Your article doesn't give added benefit over existing discussions, e.g. on StackOverflow Using App.Current.Terminate() method in Windows phone 8. Maybe you should compare the various methods that have been used on WP7 and WP8 with their pros and cons or give more use cases (SB Dev has given a sample in the aforementioned Nokia discussion thread). At least you should give a link to the MSDN docs.


influencer (talk) 09:23, 4 September 2013 (EEST)

Vaishali Rawat - WP app gets killed on pressing Back key

Hey Kunal,

AFAIK, a WP app gets killed when you press Back button (considering you are on the first page of app). If case you are not on first page, it keeps on navigating to back pages with in the app.

I have objections on your below statements. Please do the needful.

1) "it should not reside in the memory unnecessarily when user hits back to come out from the application." My view: App never stays in memory if you are pressing Back (considering you are on the first page of app). It gets killed.

2) "We all already know that, Windows Phone application stays in the memory and goes to suspended state when user hits hardware back button" My View: Again, same issue. An app goes in to suspended state when we press HOME or by launching another app.

Official documentation is at


Vaishali Rawat (talk) 10:25, 4 September 2013 (EEST)

Hamishwillee - Agreed

Hi Kunal

This would be even better if the title was "How to terminate a Windows Phone app programmatically", and you also covered WP7. The WP7 case is the difficult one since there is no supported API - here you need to do stuff like creating an exception and choosing not to catch that type of exception in the app.

If you did this then Thomas (Influencer) comment, which is true currently is no longer true.

Vaishali's points are also valid - specifically that the back button terminates the app rather than suspending it. I'll edit that in a second myself.

Thanks very much



hamishwillee (talk) 13:05, 4 September 2013 (EEST)

Kunal Chowdhury -

Thank you everyone for the feedback. I really appreciate this to improve the article.

@Vaishali, thanks for pointing out the issue. I will incorporate those changes soon.

Kunal Chowdhury (talk) 13:14, 4 September 2013 (EEST)

Hamishwillee - Half way there

Hi Kunal

that's great. I have fixed Vaishali's issue and subedited. I also added a new section for WP7 and included brief overview of one way of terminating WP7 app. All this needs now is code which shows this approach and listing of other approaches.

Hope you're OK with this!



hamishwillee (talk) 13:18, 4 September 2013 (EEST)

Kunal Chowdhury - Thank you

Hi Hamish,

Thanks for editing it. I will work on the other part and change this Wiki soon.



Kunal Chowdhury (talk) 13:22, 4 September 2013 (EEST)

SB Dev -

In fact you can exit a Windows Phone 7 application, without raising an unhandled Exception (a bad idea to begin with as it a) might lead to trouble with certification and b) will hurt your App in Store rankings, as the algorithm also takes the number of crashes into consideration). The Game class from the Microsoft.Xna.Framework namespace provides an Exit() method which works just fine on WP7. You can simply instantiate a Game object even in non-Xna-Apps and use the method to exit the App.

I did quite a lot of digging for this back when I wrote the application that uses it and I consider this to be the best way to approach the issue, if you run into one of the rare occasions where programatically terminating an App is necessary. In general this should be avoided due to the App differing from user expectations.

SB Dev (talk) 15:00, 4 September 2013 (EEST)

Hamishwillee - Great tip

Thanks SB Dev

Great tip. I've integrated it above (a new one on me!). Note that this is a wiki, so if you see a better approach you can choose to edit directly. Obviously when an article is under construction like this it is probably even better to do as you have done and make the suggestion of the author.

I think this is done now.

hamishwillee (talk) 02:56, 5 September 2013 (EEST)

Hamishwillee - except possible ....

Providing a code example for a function that can do this for both WP7 and WP8 that people can copy-paste

hamishwillee (talk) 02:57, 5 September 2013 (EEST)

SB Dev -

Possible - I guess so but it wouldn't be too pretty. As long as your App targets WP7 the XNA-Game-method will work. As soon as you change your target platform to WP8 those assemblies are no longer available. The only way around this, that I could see, would be to check for the "Teminate" method using Reflection and if it does not exist, trying to dynamically load the XNA assembly and invoking the method on the Game object instead. I'll try to do this today but can't guarantee that I'll find the time.

The rationale in providing these as comments actually was that the article still is actively developing. I have little to no problems editing wiki content directly and did so on several articles (though I have not found out wether a regular user can change the title of an articel and if so how).

SB Dev (talk) 07:43, 5 September 2013 (EEST)

Hamishwillee - Moving articles

I believe you can move articles - its done using the "Move" menu option, which is accessed from the drop down near the little eye on the right hand side of the page.


hamishwillee (talk) 11:04, 5 September 2013 (EEST)

SB Dev -

I added a terminate sample method that works on both WP7 and WP8. I tested it in the WP7 project configuration on the WP7 Emulator included with the WP8 SDK and on the Lumia 920. I also tested it in the WP8 project configuration on the 920. It would be great if someone could test it on an actual WP7 device. I can't as mine is currently used by a friend.

SB Dev (talk) 15:08, 6 September 2013 (EEST)

Kunal Chowdhury - Nice

Thanks SB Dev for the universal code snippet. BTW, will Dev Center accept those call to Reflection. Don't have idea on that, so just asking.

Kunal Chowdhury (talk) 15:10, 6 September 2013 (EEST)

SB Dev -

I haven't tried either but in principle it's only using public APIs so that at least should not be an issue. Where Reflection can be an issue is when you call certain APIs only using Reflection and it's APIs that require special capabilities (e.g. phone book access). The Store during submission checks what APIs are called and infers the required permissions that way (at least it does with WP7 - I believe it was changed for WP8 Apps). So those permissions would not be detect, therefore not be assigned to the App and executing them on the device would fail. The APIs we are using here to my knowledge don't require special permissions/capabilities so it should be fine (again - in theory).

SB Dev (talk) 15:21, 6 September 2013 (EEST)

Hamishwillee - Theory sounds fine

But if you (or anyone) find out differently then please update.

I've given this a minor update for "prettiness" and wiki style - ie using Icode template to mark up the code instead of bold.



hamishwillee (talk) 08:11, 9 September 2013 (EEST)

Influencer - Added another idea

I added another idea I used in an app.


influencer (talk) 23:22, 24 September 2013 (EEST)

SB Dev -

If I remember correctly the call of GoBack with an Empty BackStack does close the App because an Exception is raised. Something we are trying to avoid due to the repercussions associated with it regarding the Marketplace Listing and Error Reporting. It's possible though that it has changed in WP8 (didn't try it for quite some time). I'd agreee that this would be the best way to approach this as quite often what we try to do IS returning the user to the previous App when calling close (which is exactlly why we terminate the App).

SB Dev (talk) 09:34, 25 September 2013 (EEST)

Influencer - Right

Yes, that's why I wrote 'If you have removed all entries but the last'. You have to leave the back entry returning you from your first page on the stack so that no exception occurs. I changed the text again to emphasize that.


influencer (talk) 09:58, 25 September 2013 (EEST)

Hamishwillee - Good trick Thomas

Pity you can't query the backstack to find out what's left on it.

I've reworded to make this even more clear. Hope its OK.



hamishwillee (talk) 13:17, 2 October 2013 (EEST)

Influencer - Influencer - sure thanks

better now...

influencer (talk) 13:24, 2 October 2013 (EEST)

Ubaldo.Felloni - Thanks

Hi, thanks for the article.

I made some tests.

It is no possible to terminate the application without exceptions using NavigationService.GoBack()


Ubaldo.Felloni (talk) 13:14, 22 November 2013 (EET)

Hamishwillee - Ubaldo.Felloni - what tests did you try

HI Ubaldo.Felloni

>It is no possible to terminate the application without exceptions using NavigationService.GoBack()

What tests did you do? Are you saying that GoBack always raises and exception even if there is a final page on the stack (ie calling it removes the page and then raises and exception)?



hamishwillee (talk) 05:17, 25 November 2013 (EET)

Hamishwillee - Ubaldo.Felloni - thanks

More information from Ubaldo:

Main steps:

  • create a new project
  • add a button to MainPage.xaml
  • add a click event. Invoke the NavigationService.GoBack() to terminate the application.
It terminateds with the exception "System.InvalidOperationException: Cannot go back when CanGoBack is false."

hamishwillee (talk) 00:48, 26 November 2013 (EET)

Hamishwillee - Goback method removed

Thanks very much Ubaldo.Felloni for reporting this.

hamishwillee (talk) 01:39, 29 November 2013 (EET)