(X) Hide this Upcoming webinar on Feb 23rd, 10 am PST (see your local time): Building Line-of-business Applications with Silverlight & WCF Data Services
More webinar info | Register | Other webinars
Tweet @silverlightshow and win a SilverlightShow Tweet-shirt. Learn how >>
Skip Navigation LinksHome / Tips / View Tip

Tip: Detecting Network Change in Silverllight 3 Application

+ Add to SilverlightShow Favorites
6 comments   /   posted by Nikolay Raychev on Mar 19, 2009
(4 votes)
Categories: Connectivity

In Silverlight 3 you are able to check if an internet connection is present. You can also detect network changes.

See also:
Silverlight 3 as a Desktop Application (Out-of-Browser Applications)

Network availability checking:

if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())  
{  
    this.InitTasks();  

Network change detection:

NetworkChange.NetworkAddressChanged += new 
NetworkAddressChangedEventHandler(NetworkChangedCallback); 

 

private void NetworkChangedCallback(object sender, EventArgs e)  
{  
    if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())  
    {  
        this.InitTasks();  
    }  

Demo

As an example I created the following demo:


This is a simple tasks list. You can add or delete tasks. The tasks are saved on the server and the application is using WCF Service to connect to the server. Nothing special. But if you save the application for offline use you can use it even if you don't have internet connection. I'm using the isolated storage to keep copy of the data on the client and the application is working with the local copy when there is no connection. It synchronizes automatically whenever the connection comes back.

Download Source 

That's it!

Share


Comments

Comments RSS RSS
  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by Peter Gfader on May 03, 2009 05:30
    nice demo!
  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by Peter Csala on Mar 15, 2010 12:44
    I can't download the source code, because the site has got Configuration Error. Is there any mirror?
  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by nikolayraychev on Mar 16, 2010 10:00

    Hi  Peter,

    I fixed the problem, the source can now be downloaded.

  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by Peter Csala on Mar 17, 2010 00:51
    Thank you, i really appreciate your kindness.
  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by Peter Csala on Mar 17, 2010 13:49

    Hi Nikolay!

    I am developing a silverlight application, in which i have to use IsoStorage, when the network is unavailable. I tried your sample code and i bumped into the same problem, which has got my app: The NetworkAddressChanged event occurs fourtimes.

    So actually your code calls 4times the DeleteTasks method of Tasks service. Have you got any idea why this event doesn't take place only once?

    I solved this problem in my app with a simple counter, but i hope you have got an idea how to decrease this number.

  • RE: Detecting Network Change in Silverllight 3 Application  

    posted by nikolayraychev on Apr 15, 2010 11:56

    Hi Peter,

    Yes it is strange but this event fires several times and exactly 2 times when disconnecting and two times when connecting to the internet. I have no idea why.

Add Comment

 
 

   
  
  
   
Please add 4 and 2 and type the answer here: