There have been quite a few improvements under the System.Net namespace in .NET 2.0. One of those improvements is the addition of a NetworkChange class which lives under the System.Net.NetworkInformation namespace along with a whole heap of other useful network diagnostic classes.
The NetworkChange class is really a god-send for “smart client” developers who need to deal with transient network connectivity which you might find if you are using GPRS connections, wireless hotspots or even the in-house wireless LAN. The NetworkChange class exposes two events, NetworkAvailabilityChanged and NetworkAddressChanged. All you have to do is wire up to these events and get notified when things like wireless networks drop out or you switch physical segments.
To demonstrate how this worked I produced a simple little program that grabs quotes from this web-service (thanks to Swanand Mokashi) and displays them on a label on a form. That form has a status bar which displays the current connectivity status as notified by the NetworkChange class.

Now I know what you are all thinking – you are thinking – “Mitch, you are such a wonderful artist, that green light looks fantastic”. Well, as a matter of fact I wasn’t the artist, it was the icon designer that works for WinZip (great tool – go and buy it now). Ahem – back to the topic of converstation. When I disconnect the network cable or pull out the wireless adapter, I get this screen.

One interesting side note is that it appears that when using a wired connection I only seem to get notified when I next try to use the network, whereas with the wireless connection it pretty much happened straight away. The code to rig all of this up is pretty simple.
Hrm – that looks a little bit wacky doesn’t it? Well thats because I am being a good boy and am dealing with the UI threading issues in Windows Forms by marshalling the UI updates on to the right thread – the NetworkAvailabilityChanged event could come in on any thread, so you generally can’t update the UI from there. Well – thats pretty much it – if you want to see all of the code you can download it here.
Coincidentally it also uses the BackgroundWorker component triggered from the Timer to do the actual web-service call which shows how the BackgroundWorker component can be used to manage asynchronous work and get UI updates happening on the right thread.
May 15, 2005 at 12:00 am
I know I’m being pedantic and all, but couldn’t you introduce a state object here? Now look at the code bloat!
interface IStateController
{
void SetOnline();
void SetOffline();
}
class NetworkState
{
public abstract void Next(IStateController state);
}
class OnlineNetworkState
{
public override void Next(IStateController state)
{
state.SetOffline();
}
}
class OfflineNetworkState
{
public override void Next(IStateController state)
{
state.SetOnline();
}
}
now network change method looks like this:
void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new NetworkAvailabilityChangedEventHandler(OnNetworkAvailabilityChanged));
return;
}
state.Next(this);
}
ofcourse, the Form needs to implement the interface, and you could also move all of those other variables into their respective State objects. So the form really only has one instance variable called state, with is an instance of NetworkState.
Sorry, I just hate seeing state implementations like the one you posted. I know it’s a demo and all, but still…. heh.
May 16, 2005 at 12:00 am
It all depends – do I want to explain NetworkChange – or do I want to demonstrate a state transition pattern?
June 17, 2005 at 12:00 am
Thank you for using my web service so nicely

I was looking for .NET2.0 stuff and was pleasently surprised to see my name here
June 17, 2005 at 12:00 am
Hi Swanand – thanks for providing the web-service!
July 20, 2006 at 12:00 am
Thanx a bunch Mitch! Learning 2.0 stuff and this was very informative. One thing: For m_Timer_Tick, I had to change the code to:
if (this.m_Online && !m_BackgroundWorker.IsBusy)
m_BackgroundWorker.RunWorkerAsync()
in order to get it to work. Otherwise I got an error stating that “This BackGroundWorker is busy and cannot run multiple tasks concurrently.” Am I missing something here?
Thanx!
J’son
July 21, 2006 at 12:00 am
Hi J’son,
I’m on a bit of a slow link at the moment so getting to the code is difficult, but it could be a timing issue (I should have put the guard clause in), on the machine I originally wrote it on, I may not have needed it due to the precise timing of the threads.
March 8, 2008 at 7:26 am
Hi, I have used NetworkChange class and in my windows application I am trapping it’s NetworkAvailabilityChanged event. Event is fired regularly and I am able to monitor network. But my problem started when I ran the application on a PC having two NIC cards. In this scenario, this event is never fired. No matter I pluck the wire or disable the network. I tried to seach on Microsoft offficial website but was not helpful. Has anyon faced similar prob? Any help on this will be deeply appriciated.
Thanks a lot in anticipation
January 27, 2009 at 4:18 pm
Hi Mitch,
Long time no see. Dropped by this post through a google search. Had some issues with the NetworkAvailabilityChanged event fiering on worker threads, and of course ran into UI issues. You need to fix the links on this post btw.
Cheers,
Jonas
February 14, 2009 at 8:38 am
Sadly those images are gone after the great blog migration