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.Netw orkAvailability Changed.
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 NetworkAddressC hanged, 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 InitializeCompo nent(). (Note: It
doesn't work if I move it to the load event either.)
updateNetworkSt atus(NetworkInt erface.GetIsNet workAvailable() );
NetworkChange.N etworkAvailabil ityChanged +=
new
NetworkAvailabi lityChangedEven tHandler(myNetw orkAvailability ChangedHandler) ;
And here's my event handler:
private void myNetworkAvaila bilityChangedHa ndler(object sender,
NetworkAvailabi lityEventArgs args)
{
updateNetworkSt atus(args.IsAva ilable);
}
And here's the last method:
private void updateNetworkSt atus(object state)
{
//do stuff
System.Diagnost ics.Debug.Print ("***Network status changed to {0}.***",
state.ToString( ));
}
One blog I found recommended this in the event handler:
this.Invoke(new WaitCallback(up dateNetworkStat us), args.IsAvailabl e);
instead of just calling updateNetworkSt atus.
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