Search
Saturday, December 20, 2014 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Oct 1

Written by:
Friday, October 01, 2010 8:15 PM  RssIcon

miFlashlightThe way to "exit" a WP7 application seems a bit weird to me, and ya know what?  I think the VS and the MainPage agrees with me, read on for why.

miFlashlight

In writing my beautifully crafted and wonderfully artistic miFlashlight application last night (ya a little "poetic liberty there eh? LOL), I realized something really odd.  Well, to me at least. 

I wanted to quit the app when the user clicked the screen.  Easy right?  hhmmm not according to the WP7 templates and VS debugger.  Why do I say that?  Check out what VS does when I issue a NavigationService.GoBack(); command off the MainPage.xaml.

If run in Visual Studio, do this code

Uh, ok, that's only when I'm running this in Visual Studio right (see the IsAttached property check)?  Ok, I can live with that for now cause it's the first real release of the tools.  But then when you hit F5 to continue, VS continues complaining!  DOH!

Invalid operation exception

Ok, THAT'S not cool!  I can see the first one, but the second one?  Did I REALLY do something THAT bad?

Ok, here come the arguments (I'm going to do my Amazing Kreskin imitation LOL).

Cons: You're in a phone/smartphone, why are you exiting?  You DON'T exit this type of app.
  This isn't your father's Windows/DOS applications where you File|Exit man!
  The user is in COMPLETE control, even when to decide when to exit the app (ironically enough, Apple does it this way, and I've heard "the solution" is to throw an exception to get the runtime system to quit, nice trick LOL)
Pros: There should be a programatic way to "quit" an application, something as simple as Application.Exit(); would suffice.
  Would eliminate any unwarranted uncaught exceptions from happening
  Would stop developers feeling like criminals with uncaught exceptions (ok, ok, this is a slight exaggeration LOL)
  I want to be able to have an application which quits after a specific period of time (counter), upon specific criteria (low memory, no network connectivity, etc) or when the user just feels like unplugging from the binary world.
 

XNA has it!  However, TECHNICALLY by Microsoft's own Windows Phone 7 Application Certification Requirment Section 4.2.5,

"The application must not call any APIs in the MIcrosoft.Xna.Framework.Game assembly...when using any methods from the System.Windows.Controls namespace"

hhhmmmmm strike one, can't use the other framework as a "work around."

*****

Some people are suggesting "just throwing an exception" and the WP7 OS will throw you out.  OH that's nice, the way to leave your friend's house is to got the washroom and stink it up so much they ask you to leave?  Ok, a SLIGHT exaggeration, but really, is explicitly throwing an exception like this the best way?

In fact, the The BIGGEST reason for having an explicit API for this can be found in the Windows Phone 7 Application Certification Requirments document itself!  Section 5.1.2 states:

"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." 

Therefore, any attempts to use exceptions as regular programming practises (aka tricks/hacks) are TECHNICALLY going to result in a failed application.  It COULD be argued that this is acceptable but when was the last time you actually coded your app to throw an exception to avoid doing something, and were rewarded for it?  Ok, ok, so you did it before, but that doesn't make it right! LOL  There should be an Application.Exit() IMHO.

The Amazing Kreskin

This has gotten quite a bit of attention on the WP7 forumsPeter Torr, WP7 program manager even created a specific blog about it.  Talk about right from the horses mouth!  He even talks about my method of throwing an exception (#4).  Ok, Peter says this, and I'm quoting cause I want to have this as a reference IF my app should fail certification because of this.

"We can quickly answer (b) by saying you should follow #4 above - simply ignore the error in your UnhandledException event and the application will be terminated for you."

Peter does continue with a pattern/strategy to great your own Exception class to throw and catch, which isn't a bad idea now that I think of it.  I'll have to try it.

Peter continues to say the API way is not there right now, and that's on purpose, BUT he does leave the door open to changes in the future.  I can live with that.

"That said, it doesn't mean we can't or won't add such a method in the future if we get a lot of feedback that it really is necessary"

So what's your call?  What do YOU think should be done here?  Continue to throw the exception?  Or have an Application.Exit()?  I do hope you leave a comment here with your vote.  Now it's time to grab a coffee and get coding!

PS COOL!  I just updated my code trying out Peter Torr's solution and it's nice.

    // Code to execute on Unhandled Exceptions
    private void Application_UnhandledException( object sender, ApplicationUnhandledExceptionEventArgs e )
    {
      if( !(e.ExceptionObject is ApplicationNeedsToExitException) )
      {
        if( System.Diagnostics.Debugger.IsAttached )
        {
          // An unhandled exception has occurred; break into the debugger
          System.Diagnostics.Debugger.Break();
        }
      }
    }

 Oh, you're probably wondering what's my ApplicationNeedsToExitException look like?  hhmmmmm very complicated, very Java-ish!  DOH!

  public class ApplicationNeedsToExitException : Exception
  {
  }

 

 

Resources
PCHenry.com: WP7 Marketplace is about to start accepting WP7 apps, are you ready?

Windows Phone site

Windows Phone Site: Windows Phone 7 Application Certification Requirements (PDF download)

MSDN: Game.Exit Method

MSDN: How to exit an application?

Peter Torr: Program Manager on WP7 team

Peter Torr: Exiting a Windows Phone Application

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

12 comment(s) so far...


Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

I agree, using exceptions for any sort of flow logic is not a good thing.
But, in terms of a phone app, the model is very different from a desktop app. The desktop model is more deterministic in the app's ability to run, and is required to manually release resources. Technically the app isn't running when you switch to another app. There'a no reason to release resources in the phone model. It's kinda like having a exit button on a web page.

By Peter Ritchie on   Saturday, October 02, 2010 11:25 AM

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Oh lordy... only in Microsoft land would you throw an exception as a solution to exit an application... Smart phone applications are a different paradigm, its like MS half-arsed attempt with make developers "use" thick-client techniques on the Web (aka ASP.NET Webforms).

Think about it as not trying to fit the 'desktop' world into the smart phone mobile world (you mentioned it yourself, in your article :) ). There is no 'exit' button required in the applications as that is what the little 'hardware' button is for on these devices (and you receive the proper events that your closing if you require to do any cleanup... same hold true for Android and iOS). From a UI design stand-point having a 'close app' option (as well as the hardware 'close' button) is really just duplicating functionality (might as well have 2 "Exit" menu options under a "File" menu bar :).

As for uncaught exceptions and the general throwing of exceptions willy-nilly... well you know where I stand on that :)

By Shane on   Saturday, October 02, 2010 12:30 PM

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

re Peter, exit button on a webpage
haha but yet we continue to see requirements/specs which treat web apps/pages exactly like thick client apps! DOH! I'm VERY interested to see in a year from now how specs are handed over to dev for these very small screen devices.

re Shane, only in MS-land, uh, how is it done on the iPhone? hhmmm interesting link (even if changed in iOS4, at one time THIS was current).
http://developer.apple.com/library/ios/#qa/qa2008/qa1561.html
How do I programmatically quit my iPhone application?
A: There is no API provided for gracefully terminating an iPhone application.
There is a warning that you can use exit method, BUT according to stackoverflow, this is a sure-fire way to get rejected. Interesting.
http://stackoverflow.com/questions/355168/proper-way-to-exit-iphone-application

re willy-nilly
haha I understand just "whipping out an uncaught exception" is bad. But in this case, it's just one of those things you know smells but you have to to "to just get it to work." Not pretty but it works and can afford you time to move onto other features, like da clapper! :> HAHA

Both very good points, and I love the discussions from you guys! Thank you for reading and commenting! I was at the WP7 presentation today at Algonquin College and the excitement in the room was very cool to see students are into this, even quite a few iPhone devs in there kicking some tires! Too cool too see!

By phenry on   Saturday, October 02, 2010 8:55 PM

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

I think the point I was more trying to get across is that in this "smart phone"/mobile application world where an 'exit'/'task switcher' style hardware button exists (and is part of the general expected 'navigation' design paradign) there should no reason for the application to need to offer another method to close itself. There should be no reason to look for a way of hacking a close option... Rethink your applications design/architecture if this is really required...

Just discussion points :)

By Shane on   Sunday, October 03, 2010 4:31 PM

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

As for the "only in Microsoft-land"... This was targeted towards an actual Microsoft Program Manager talking about the validity of using exceptions as an actual design option for "missing functionality". :)

By Shane on   Sunday, October 03, 2010 4:32 PM

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Thanks for the clarification Shane. However, the one example I'm imitating (miFlashlight) is one such example of where I want to programatically control when the apps exits.

re rethink the app
hhmmm interesting. At first I thought NO! That's the way....but then...hhhmmmm if the device is going to Lock the phone, and it goes back to the "start screen" afterwards (after unlocking), then maybe the proper solution is to give the user a countdown (which they can of course change) and disable the phone locking until then, then lock the phone. Interesting. I have to see if WP7 offers an API to lock the phone? Might be a way to differentiate myself from the onslaught of other flashlight apps! haha

re MS-land
DOH! haha not quite sure what to say to that. Well, other than thanks for commenting! Have a good week guys!

By phenry on   Sunday, October 03, 2010 4:36 PM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

We have a business need to prevent a user from using an application if their IMEI number is invalid. Having an Application.Exit() method would be the ideal way to exit the application, rather than throwing an exception.

By Jason on   Monday, February 21, 2011 9:42 AM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Hi Jason, unfortunately with Microsoft's and WP7 user centric philosophy, I think we're not going to get this for a while. But who knows, maybe?

Is there some other way you can architect your product to not require the Exit button/functionality? I've been playing with mine a bit more and realized I didn't need it (not nearly as bad as I thought I did). That might help (until MS gets around to giving us the Exit() LOL).

Thanks for commenting, have a great Family Day! (if you're in ON, if not, have a great day and good week too!)

By Jason on   Monday, February 21, 2011 9:54 AM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Very disturbing I had this problem many times aswell and I thought it was its own functionality that didn't meet the expectations. Although I recently found this site that solved this. I'll be checking the updates to make sure the functionality will be correct.

By Paul Casin on   Wednesday, October 12, 2011 9:54 AM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Hi Paul, thank you for finding my blog and commenting! Glad to see people are still getting some use out of my WP7 entries (I know I need them to finger flick my brain sometimes too haha). I hope to generate more material to convince you to come back and visit. Thanks again and have a good end of week!

By Peter Henry on   Thursday, October 13, 2011 10:02 AM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Hi, thanks for this post but i found a safer way to do it.

The answer is fairly simple, just do the following:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
MessageBoxResult res = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
e.Cancel = false;
base.OnBackKeyPress(e);


}
}

Visit austinejei.blogspot.com/2012/06/exiting-wp7-application.html for more information.

By Augustine Adjei on   Friday, June 22, 2012 6:54 AM
Gravatar

Re: The way to "Exit" a WP7 app just seems wrong to me, is it just me?

Very interesting Augustine. I read your blog as well, very cool! Are you getting into WP7 dev now? I'm curious to find out more about the base.OnBackKeyPress(e), I thought I tried that and got an error, but I'll have to revisit another time. Meanwhile, I tried redesigning my apps to not necessarily need an explicit exit button, seems to work for now. Thanks again for visiting AND commenting! Have a good weekend!

By Peter Henry on   Friday, June 22, 2012 9:56 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement