1

I want to close my app if network not available.

I check network in App.cs:

private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                //close my app
            }
            else
            {
                 //continue to work
            }
        }

Is there a better way to do it?

Thanks in advance.

Alexandr
  • 1,891
  • 3
  • 32
  • 48
  • I don't think that just closing your application is a good solution. You should at least notify the user first. – vonLochow May 22 '13 at 07:49
  • Of course, before closing, I will show a message that will explain to the user why the application will now close – Alexandr May 22 '13 at 07:51
  • 1
    This isn't a great idea in `Launching`. GetIsNetworkAvailable can take over 10s. In that case you'll be killed by the OS before you even start as you ran out of time to show your first page. – Paul Annetts May 22 '13 at 11:38
  • Wow! I did't know, thank you! – Alexandr May 22 '13 at 11:59

2 Answers2

1

just add reference to Microsoft.Xna.Framework.Game i'm sure you can achieve exit with this code and it will be ok. if you wanna show message box you have to do it in main page

what i would do:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
               MessageBoxResult m = MessageBox.Show(Sorry, no internet connection is available.do you want to exit the application , "Oops...", MessageBoxButton.OKCancel);
                if (m == MessageBoxResult.OK)
                {
                    var g = new Microsoft.Xna.Framework.Game();
                        g.Exit();
                }
            }           
        }

you should provide a "gentle" way for closing

5.1.2 - App closure

The app must handle exceptions raised by the any of the managed or native System API 
and not close unexpectedly. During the certification process, the app is monitored 
for unexpected closure. An app that closes unexpectedly fails certification. The app
must continue to run and remain responsive to user input after the exception is
handled.

for more information visit this link

Community
  • 1
  • 1
Swift Sharp
  • 2,604
  • 3
  • 30
  • 54
  • Thank you for your response! It looks like this is what I need, but I can't add reference to Microsoft.Xna.Framework.Game? How can I add reference in WP 8 project? – Alexandr May 22 '13 at 07:31
  • add reference > browse >`your directory in my case` Program Files (x86)\Microsoft XNA\XNA Game Studio\v4.0\References\Windows\x86 – Swift Sharp May 22 '13 at 07:41
  • 1
    Visual Studio 2012 gives an error "In the project can not add a reference to a later version or incompatible link", but I think I can resolve this problem, thank! – Alexandr May 22 '13 at 07:49
  • This API isn't available on WP8. You'll have to use Application.Current.Terminate(). – Paul Annetts May 22 '13 at 11:37
  • I found a way how to do it http://stackoverflow.com/questions/10881005/how-to-install-xna-game-studio-on-visual-studio-2012, but it's hard. – Alexandr May 22 '13 at 11:57
1
Application.Current.Terminate();
Alaa Masoud
  • 7,085
  • 3
  • 39
  • 57
  • Thanks you, I checked this method and it works, but I found post in SO http://stackoverflow.com/questions/14460571/using-app-current-terminate-method-in-windows-phone-8 and in the post written that "ApplicationClosing even handler won't be raised after it", I think that's not really what I want, but in any case, thanks! – Alexandr May 22 '13 at 07:29