Reputation: 43321
I am using this simple way to detect network connect/disconnect events:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
...
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
RaiseNewtorkChange();
}
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
RaiseNewtorkChange();
}
static void RaiseNewtorkChange()
{
...
}
The problem is that sometimes NetworkAddressChanged
event just stops working, after it was fired several times. Does anyone have an idea why this can happen?
Alternatively, is there another way to handle network connect/disconnect events, using C# or C/C++. Maybe there is such functionality in Windows API or WMI? I need notification on LAN/WiFi network connecting/disconnecting, without polling.
Upvotes: 2
Views: 1479
Reputation: 10418
It seems to me that you need to use the Native Wifi API.
Take a look at the function WlanRegisterNotification, in particular this notification:
WLAN_NOTIFICATION_SOURCE_ACM:
Registers for notifications generated by the auto configuration module.
Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the wlan_notification_acm_connection_complete and wlan_notification_acm_disconnected notifications are available.
Disclaimer: I have used Native Wifi API in the past, but I have never used this particular feature.
Upvotes: 1