How to terminate your Windows Phone application programmatically?

How to terminate your Windows Phone application programmatically?


21/08/2012 Update: 

There is another way to close the Windows Phone application without throwing exception see more details in my next post.

We want full power!

Its known that in Windows Phone API there is no easy way to terminate the current application from your code.

I guess makers of the platform did not wanted us developers to mingle with the application lifetime too much, phone should decide when an app is not needed and terminate it.

But i just simply cannot help myself, i always like to have full control, for example in my Offline Browser application i want to end the app if user presses the hardware Back Key multiple times in short time intervals.

Because there is this annoying situation when user goes navigating between the pages in your app (main screen, then settings, then about screen,

then again main screen etc) and then when he wants to terminate it he need to click the hardware back button N number of times in order to close it.

And even in some other situations its needed to force close the app, for example if some change in application settings requires app restart etc.

Show Us The Code!

So i decided to change this and wrote this simple AppLifetimeHelper that allows you to terminate your current app without any delay.

How it works is that it uses Application.Current.RootVisual and casts it to PhoneApplicationFrame that has RemoveBackEntry method that basically simulates

the users hardware Back Key press and then in endless loop it calls this method until the navigation back stack of the app is empty.

Then all we need to do is to call the GoBack method of the PhoneApplicationFrame and it will terminate the app since there are no more entries in the app Back Stack.

Simple or what?   😎

So here is the code if you AppLifetimeHelper that you can freely use in your apps if you need this functionality:


using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;

namespace Roboblob
{
    public class AppLifetimeHelper
    {
        public void CloseApplication()
        {
            ClearApplicationNavigationBackStack();
            Root.GoBack();
        }

        private PhoneApplicationFrame _root;
        private PhoneApplicationFrame Root
        {
            get
            {
                if (_root == null)
                {
                    _root = Application.Current.RootVisual as PhoneApplicationFrame;
                }

                return _root;
            }
        }

        private void ClearApplicationNavigationBackStack()
        {
            if (Root == null)
            {
                return;
            }

            try
            {
                while (Root.BackStack.Any())
                {
                    Root.RemoveBackEntry();
                }
            }
            catch
            { }
        }
    }
}

And if you are really lazy today…

Here is the download link to the Visual Studio solution with Windows Phone application that uses this helper to terminate it self when a button is clicked…

5 thoughts on “How to terminate your Windows Phone application programmatically?

  1. I have a couple of solutions that I’m going to share in a blog series on my website after I launch the new redesign. I will return to let you know when I get the blog posted with full drop-in code that can be used from any of your apps with just a few lines of code to accompany the drop-in class. To do it well, you also have to be able to interrupt the shutdown and handle any state management.

    The idea of creating an exception upon the invocation of the exit command might appear on the surface as acceptable as it gets the desired result, if the certification team encounters this unhandled exception during testing, you’re going to fail and then have to redesign to remove the functionality until you come up with another solution. It is best to avoid extra work in the first place.

    Thomas Mullen

Leave a Reply