Better way to check for an network connection on WP7

Last month I described a method for determining whether or not you have a network connection from your Windows Phone 7 app:

bool hasNetworkConnection =
              NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;

Unfortunately there is a serious problem with this approach.

It turns out that accessing the NetworkInterface.NetworkInterfaceType property is a blocking call.  The property will not return until is has a result and this can take some time.  It might be a second or two when you have a connection available, however it can take up to 20 seconds (in my experience) when you don’t have any connection at all.  All this time the UI thread is blocked.  Not good!

As a side note this is a bad piece of API design on Microsoft’s part.  A property should always return immediately. Not doing so breaks two of Microsoft’s own usage rules regarding properties:

  1. functionality should not be implemented as a property if it “has an observable side-effect” – blocking the UI for up to 20 seconds is certainly an observable side effect.
  2. and should not be a property if “the operation is expensive enough that you want to communicate to the user that they should consider caching the result.”

Nevertheless, like it or not, accessing NetworkInterface.NetworkInterfaceType is a blocking call.  We have to just deal with it.  Fortunately its not too hard to fix – we just need to make sure that if we ever access NetworkInterface.NetworkInterfaceType we do it asynchronously on a background thread.  Here is a simple example which does this:

public static void GetNetworkInterfaceType(Action callback)
{
   ThreadPool.QueueUserWorkItem(o =>
             {
                 var interfaceType = NetworkInterface.NetworkInterfaceType;
                 ((Action)o)(interfaceType);
             }, callback);
}

Bear in mind with this code that your callback will be executed on a background thread so you will need to marshal back to the UI thread if you are doing anything that isn’t thread-safe – like updating the UI.

A more fully featured wrapper around this idea has been created by Gabor Dolhai, if you really need to get serious about network connection detection.

Another option to bear in mind is the NetworkInterface.GetIsNetworkAvailable() method.  If you just need to detect if you are connected to any network and don’t really care what kind of connection you have you can use GetIsNetworkAvailable().

bool hasNetworkConnection = NetworkInterface.GetIsNetworkAvailable();

The method does return immediately and does not block the UI so can be called safely on the UI thread.   Beware however that this method will always return true in the emulator regardless of your actual connectivity.

Advertisements

9 thoughts on “Better way to check for an network connection on WP7

  1. Pingback: How to check if you have an network connection on WP7? « C Is For Coder

  2. GetIsNetworkAvailable always returns true because it is always true. You have an Ethernet connection to the host…remember, GetIsNetworkAvailable is *not* about Internet connectivity, but network connectivity.

  3. Thanks for this post, I should really try it because I followed the previous one and then I deploy my app to the phone with your last suggestion. Then, I took my phone around town to see any network issues, and I saw that in blind spots, the application took longer, and the user interface froze until I had a connection.

  4. Pingback: The puppet who has cut his strings - Dark Genesis - XNA UK User Group

  5. Pingback: Getting ready for the Windows Phone 7 Exam 70-599 (Part 1)

  6. Pingback: Windows Phone 7 et les tests unitaires asynchrones avec RestSharp « So@t blog

  7. Pingback: Week View startup time when no network connection exists | All about apps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s