Abnormal exit of applications caused by MessageBox processing by Windows Phone and Exit applications using code

Source: Internet
Author: User

Some people may think this is an old-fashioned question. It is indeed a known issue, but I found that some people are still asking this question, so I would like to share it with you here.

First, after MessageBox is displayed, if the user ignores the message page, it will take about 10 seconds to wait.ProgramIt will automatically exit. (This phenomenon will not occur during debug)

First, let's briefly analyze the cause of this problem. First, why is there no problem in the application during debugging, it is very simple. When debugging an application, it is likely that a breakpoint stays for more than 10 seconds (a return value blocks the main thread). However, when the application is not debug, the SDK will think of yourCodeIf there is a problem, the system will force exit.

In fact, the method to solve this problem is very simple. Now that you know the cause of this problem, use an Asynchronous Method (thread) to solve this problem.

 Protected   Override   Void  Onbackkeypress (system. componentmodel. canceleventargs e) {e. Cancel = True  ;  Base . Onbackkeypress (E );  This . Dispatcher. begininvoke ( Delegate  {  If (MessageBox. Show ( "  Do you want to exit the program?  " , "  Exit  " , Messageboxbutton. okcancel) = Messageboxresult. OK ){}});} 

Of course, some people may ask how to exit the application if we set E. Cancel to true before calling MessageBox? This is also an old problem (a problem left behind in the WP7 era). Here I also sum up my experience and see some methods on the Internet.

Windows Phone 8 update this method can directly end the application.

 
Application. Current. Terminate ();

However, this method does not call the onnavigatedfrom event on the page or the application_closing event in the app. Therefore, you must save user data before calling this method.

Windows Phone 7

First, game. Exit () in xNa is not recommended because there will be problems during App Store review, resulting in failure to store.

Currently, the only reliable method is to handle it by throwing an exception and handling it in the application_unhandledexception event in the app file:

There are two ways to throw an exception on the network:

1. Custom exceptions

 Private   Class  Quitexception: exception {}  Public  Static   Void  Quit (){  Throw   New  Quitexception ();}  //  Code to execute on unhandled exceptions  Private   Void Application_unhandledexception ( Object  Sender, applicationunhandledexceptioneventargs e ){  If (E. exceptionobject Is Quitexception)  Return  ;  If  (System. Diagnostics. Debugger. isattached ){  //  An unhandled exception has occurred; break into the debugger  System. Diagnostics. Debugger. Break ();}} 

Use app. Quit () to exit the application.

 

2. Use navigationservice. Goback (); to exit the application

 Protected   Override   Void Onbackkeypress (system. componentmodel. canceleventargs e) {e. Cancel = True  ;  Base  . Onbackkeypress (E );  This . Dispatcher. begininvoke ( Delegate  {  If (MessageBox. Show ( "  Do you want to exit the program?  " , "  Exit " , Messageboxbutton. okcancel) = Messageboxresult. OK ){  While  (Navigationservice. backstack. Any () {navigationservice. removebackentry () ;}navigationservice. Goback ();}});} 

Similarly, handle this exception in application_unhandledexception.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.