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?   8-)

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…