My application needs to know when the network availability has changed and
do some stuff when it does.
I wrote a small WinForms app to test System.Net.NetworkAvailabilityChanged.
It works great.
I put the same logic in my main application, and the event is never caught
by my handler.
If I put in NetworkAddressChanged, it *is* fired and caught. But I don't
need to know if the address changed, I need to know if the user is online or
not.
Here's my code:
In the constructor of my main form after InitializeComponent(). (Note: It
doesn't work if I move it to the load event either.)
updateNetworkStatus(NetworkInterface.GetIsNetworkA vailable());
NetworkChange.NetworkAvailabilityChanged +=
new
NetworkAvailabilityChangedEventHandler(myNetworkAv ailabilityChangedHandler);
And here's my event handler:
private void myNetworkAvailabilityChangedHandler(object sender,
NetworkAvailabilityEventArgs args)
{
updateNetworkStatus(args.IsAvailable);
}
And here's the last method:
private void updateNetworkStatus(object state)
{
//do stuff
System.Diagnostics.Debug.Print("***Network status changed to {0}.***",
state.ToString());
}
One blog I found recommended this in the event handler:
this.Invoke(new WaitCallback(updateNetworkStatus), args.IsAvailable);
instead of just calling updateNetworkStatus.
That doesn't work either, and I'm not sure why I would use that over just
calling it.
Any help or advice would be greatly appreciated.
Thanks,
RobinS.
GoldMail.com