(X) Hide this Last chance to take part in SilverlightShow ebook survey and win 3 free ebooks 'Windows Phone 7  for Silverlight Developers! Take the survey >>
Join the upcoming webinar: Deep-DIVE in Silverlight FIVE by Gill Cleeren. August 24, 10 am PDT (check your local time).
Sign up for this session | Full agenda | Watch recordings of past webinars
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: Detecting Network Change in Silverllight 3 Application

(4 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   18
Comments:   58
More Articles
7 comments   /   posted on Mar 19, 2009
Categories:   Out-of-Browser

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!


Subscribe

Comments

  • -_-

    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?
  • nikolayraychev

    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.

  • nikolayraychev

    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.

  • -_-

    RE: Tip: Detecting Network Change in Silverllight 3 Application


    posted by Harry on 13:07

    Anyone has an updated source code for this trick? This is old but still is very useful. The source code has some bugs though.

    Mikos,

Add Comment

Login to comment:
  *      *       
Login with Facebook