Anatoly Lubarsky

Logo
MSSQL, .NET, Design. Life and Music

How to Monitor Network Connectivity in .NET

There is a common problem with smart client applications that they need to implement monitoring network connectivity. Common scenario - if the computer is connected - send data to the server, otherwise - serialize data to disk, then wait for network connectivity becomes available and when the network is available - send serialized data to the server in the background.


.NET 2.0 introduced System.Net.NetworkInformation with NetworkAvailabilityChanged event, so there is no need to reinvent network monitor behavior by hand any more :)


Use the right namespace :)


using System.Net.NetworkInformation;

Now it is possible to check connectivity on demand...


bool connected = NetworkInterface.GetIsNetworkAvailable();

Or subscribe to event in some place....


NetworkChange.NetworkAvailabilityChanged += OnAvaiabilityChanged;

Implement like so:


private void OnAvaiabilityChanged(object sender, 
    NetworkAvailabilityEventArgs args)
{
    this.UpdateStatus(args.IsAvailable);
}

private void UpdateStatus(bool connected)
{
    if (connected)
    {
        ...
    }
    else
    {
        ...
    }
}

Enjoy :)


Related Posts:

Friday, June 08, 2007 8:07 PM

Comments

# Interesting Finds: June 9, 2007

6/9/2007 10:12 AM by Jason Haley

# On en apprend tous les jours : NetworkChange.NetworkAvailabilityChanged et NetworkInterface
Dans la série " on en apprend tous les jours " : la classe NetworkChange , encore une dont je n'avais

6/9/2007 4:46 PM by CoqBlog

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)