Windows Phone application of MessageBox leads to abnormal withdrawal and exit th

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

See the topic may be some students feel that this is a a commonplace talk of an old scholar of the problem, which is really a known issue but I found there are still students in asking this question, so here we summarize the share.

After the first problem the MessageBox display, if the user does not ignore the Message page, probably about 10 seconds the program will automatically exit. (this phenomenon does not appear in the Debug)

Briefly analyze the reason of this problem, firstly, why not in the application of Debug when the problem, very simple when we debug the application is likely to be a breakpoint to stay for more than 10 seconds (a return value of blocking the main thread), however the application of this phenomenon in the non Debug case, SDK will think you the code will have problems forced exit.

Method the solution for this problem is very simple, since we know the cause of this problem, using 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? ", "Sign out", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 
        { 
          
        } 
    }); 
}

image

Though there are certainly students will be asked to call MessageBox before the e.Cancel set True that how to quit the application? This is an old problem (WP7 era Legacy), here I also summarize the experience, some methods from the Internet to see.

Windows Phone 8 update this method can directly end application.

Application.Current.Terminate();

But this method here does not call the page's OnNavigatedFrom event and App event in Application_Closing, so before calling this method should pay attention to the preservation of user data.

Windows Phone 7

First, in XNA (Game.Exit) is not recommended because in the app store approval process will encounter problems, leading to not store.

At present the only reliable method is by throwing an exception and in the App file in the Application_UnhandledException event handling it:

The network throws an exception in two ways

1 abnormal custom

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(); 
    } 
}

Finally, using the App.Quit () exit application.


By using NavigationService.GoBack (2); exit 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? ", "Sign out", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 
        { 
            while (NavigationService.BackStack.Any()) 
            { 
                NavigationService.RemoveBackEntry(); 
            } 
            NavigationService.GoBack(); 
        } 
    }); 
}

Similarly, in the Application_UnhandledException deal with this anomaly.

image

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lorry at December 01, 2013 - 7:00 PM