(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Tip: Network change events in Silverlight

(4 votes)
Gill Cleeren
Gill Cleeren
Joined Apr 02, 2010
Articles:   63
Comments:   6
More Articles
0 comments   /   posted on Jul 15, 2011
Categories:   General

Since Silverlight 3, it’s possible to detect changes in the state of network availability. This can be useful to check whether or not a service call can be executed. In combination with the possibility to build a Silverlight application that runs out-of-browser, this feature opens up ways to build occasionally connected system. In this article, we’ll look how we build an application that detects changes in the network state.

The code for this article can be downloaded here.

Check network state

For this article, I’ve put together a very basic interface that can be seen below. The application knows whether it’s online or offline. Also, it knows whether or not it’s running inside the browser or not.



The XAML for this interface is shown below.

<Border BorderBrush="Red" Name="MainBorder"  
    BorderThickness="4" Width="400" Height="200">
    <StackPanel Orientation="Vertical">
        <TextBlock FontSize="32" Name="ConnectionStatusTextBlock"
            HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock FontSize="32" Name="ApplicationStatusTextBlock"
            HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

Checking changes in the network can be done using the NetworkChange class. This is a very simple class: it defines just one event called NetworkAddressChanged. This event is triggered by Silverlight automatically when it notices that the IP address has changed, meaning that either a network card was plugged-in or unplugged. To react to changes, we need to subscribe to this event. In the sample, we are doing so using the following code:

NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
private void CheckNetworkStatus()
    if (NetworkInterface.GetIsNetworkAvailable())         
        MainBorder.BorderBrush = new SolidColorBrush(Colors.Green);
        ConnectionStatusTextBlock.Text = "Connected";
        MainBorder.BorderBrush = new SolidColorBrush(Colors.Red);
        ConnectionStatusTextBlock.Text = "Not connected";        

The NetworkChange class is used to detect changes, but in the NetworkChange_NetworkAddressChanged event handler, we don’t know whether or not we have a network connection available. As can be seen, no specific EventArgs is being passed in to get in this value. Instead, we need to use another very simple class called NetworkInterface. This class again only defines one static method called GetIsNetworkAvailable(). If it returns true, it means that we have a network connection. In fact, it returns true if any available network interface is marked "up" and this interface is not a loopback or tunnel interface.

Check application state

Next to checking whether or not we have a network connection available, we can also detect whether or not the Silverlight application is running inside of the browser or if it’s running as an out-of-browser application. The following code allows us just to do so:

Application.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);
void Current_InstallStateChanged(object sender, EventArgs e)
private void CheckInstallState()
    if (Application.Current.IsRunningOutOfBrowser)         
        ApplicationStatusTextBlock.Text = "Running out-of-browser";         
        ApplicationStatusTextBlock.Text = "Running in the browser";        

The InstallChanged() event is triggered by Silverlight when the application goes from in-browser to out-of-browser state.

Practical usages of these events

One might ask where we can make use of these events. The most practical use I see for these are occasionally connected systems. For example, assume we have built an out-of-browser Silverlight LOB application used by people on the road. The application allows for basic inputting of data and displaying of data. The application gets and retrieves data over a service. Since the application is used by people on the road, they get their data over a 3G network connection. Where I live (in Belgium), 3G coverage is good, but there are areas where no data connection is available.

The Silverlight application can then use the NetworkAddressChanged event in combination with the GetIsNetworkAvailable method to see if 3G is available. If it is, data will be sent directly to the service. However, when no connection is available - the application knows this – the data will be stored locally. A way of doing this is storing the data in XML in the isolated storage. When the application then detects that network becomes available again, it will start synchronizing data with the service again.

Another possible area where this event can be used is for example a player. Assume you have an application that streams a radio stream to the client. To give the user a good experience, we don’t want him to ever have to miss our radio station. Therefore, in the background, we can download some podcasts of a past show (again to isolated storage). When the application detects that the network is no longer available, it will of course no longer be able to play the audio stream. Instead, it will switch to the downloaded podcasts.

Finally, this event can in general be used to allow us to know whether or not we are connected or not. Based on this information, we can for example disable buttons that would normally invoke a call to a service. Since we know we don’t have any connection available, we don’t risk that the application might be waiting for a time out to be happening when calling a service with no network available.


In this article, we looked at the possibility to detect changes in the network being available in a Silverlight application through the use of the NetworkAddressChanged event. We also looked at some practical usages of this event.

About Gill

Gill Cleeren is Microsoft Regional Director (www.theregion.com), Silverlight MVP (former ASP.NET MVP), INETA speaker bureau member and Silverlight Insider. He lives in Belgium where he works as .NET architect at Ordina. Passionate about .NET, he’s always playing with the newest bits. In his role as Regional Director, Gill has given many sessions, webcasts and trainings on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd Berlin 2010, TechDays Belgium, DevDays NL, NDC Oslo Norway, SQL Server Saturday Switserland, Spring Conference UK, Silverlight Roadshow in Sweden… He’s also the author of many articles in various developer magazines and for SilverlightShow.net. He organizes the yearly Community Day event in Belgium.

He also leads Visug (www.visug.be), the largest .NET user group in Belgium. Gill recently published his first book: “Silverlight 4 Data and Services Cookbook” (Packt Publishing). You can find his blog at www.snowball.be.

Twitter: @gillcleeren



No comments

Add Comment

Login to comment:
  *      *