Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a event to detect Internet connect/disconnect

We are developing a .NET application where one of the requirements is to monitor whether the system is connected to the internet or not.

We were able to get a .NET event for "ethernet cable disconnect", but if the modem is reset then this event does not get triggered. I don't want to keep pinging some URL to get this done since it will add considerable performance overhead. Is there any .NET event which can check whether the system is connected to the internet or not?

There is an icon in system tray which shows a cross sign or limited connectivity sign when the system is not conncected to the internet. That suggests Windows knows, and I want to trap that event.

like image 926
Nuetrino Avatar asked Dec 16 '10 05:12

Nuetrino


People also ask

How do I find network disconnection in Event Viewer?

Within Event Viewer, navigate to each log: System: Expand Windows Logs; System will be listed underneath. Filter the log for networking entries (for example, for source "Diagnostics-Networking", or specific Event IDs that apply to wireless, 802.1x, WPA2). You can look up Networking Event IDs online.

How do I track an intermittent Internet connection?

The most accurate way to detect intermittent network problems is by using a continuous Network Monitoring Software, like Obkio. Obkio's Network Monitoring Solution continuously measures your network performance by sending and monitoring data packets through your network every 500ms using Network Monitoring Agents.


3 Answers

You can use the NetworkChange class, with the NetworkAvailabilityChanged event:

NetworkChange.NetworkAvailabilityChanged += myNetworkAvailabilityChangeHandler;

Since it's a system event, make sure you delete the event when you're finished, see this post here: You need to be careful about using event handler for NetworkChange

like image 174
Simon Mourier Avatar answered Oct 19 '22 04:10

Simon Mourier


This is all covered (including the difference between being on the network and having the network connect you to the Internet) at http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx. I hope you meant to put that Windows 7 tag on your post, because all this is pretty new.

The key is INetworkListManager.get_IsConnectedToInternet() which pretty much does what it says on the tin. You have to jump around a bit to register for the events etc. The Code Pack wraps some of that up for you and has a network sample you can adapt.

like image 29
Kate Gregory Avatar answered Oct 19 '22 04:10

Kate Gregory


I was able to solve this problem to some extent. I was able to find some sample code in Code project http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=34650. Thanks all for the replies.

especially the article link which was posted by Ms Gregory helped me a lot.

like image 3
Nuetrino Avatar answered Oct 19 '22 02:10

Nuetrino