/// <summary> /// get an enumerable of network interfaces that have disconnected since the last status. /// </summary> /// <param name="lastStatus">last network status snapshot</param> /// <returns></returns> public IEnumerable <NetworkInterface> AddressChange(NetworkStatus lastStatus) { // enumerate the current snapshot values. var result = new List <NetworkInterface>(); foreach (var pair in _status) { // check this network interface was in the last snapshot: if (lastStatus._status.ContainsKey(pair.Key)) { //if (pair.Value.Interface.Description.ToUpper().StartsWith("MICROSOFT")) continue; // check if the status changed... var lastAddress = lastStatus._status[pair.Key].Interface.GetIPProperties().UnicastAddresses; var address = pair.Value.Interface.GetIPProperties().UnicastAddresses; //lastAddress[0].Address.ToString(); try { if (lastAddress[0].Address.ToString() != address[0].Address.ToString()) { // this network interface has changed since the last snapshot: result.Add(pair.Value.Interface); } } catch (Exception exception) { continue; } try { if (lastAddress[1].Address.ToString() != address[1].Address.ToString()) { // this network interface has changed since the last snapshot: result.Add(pair.Value.Interface); } } catch (Exception exception) { continue; } } else { // this network interface is new.. result.Add(pair.Value.Interface); } } return(result); }
/// <summary> /// list of network interfaces that have become faster. /// </summary> /// <param name="lastStatus"></param> /// <returns></returns> public IEnumerable <NetworkInterface> Faster(NetworkStatus lastStatus) { foreach (var status in _status) { if (lastStatus._status.ContainsKey(status.Key)) { // if the last recorded interface speed was less than the current speeed, then // this interface is now faster. if (lastStatus._status[status.Key].Speed < status.Value.Speed) { yield return(status.Value.Interface); } } } }
/// <summary> /// the task given to the monitor thread. /// </summary> private void MonitorTask() { // loop while the run flag is true. while (_run) { try { // has the last status been taken? if (_last == null) { // snapshot the current status. _last = new NetworkStatus(); // sleep for the duration of the poll interval. Thread.Sleep(_waitInterval); // run to the next iteration. continue; } // get the current network status: var current = new NetworkStatus(); // test for changes and raise events where neccessary. if (NetworkInterfaceConnected != null && _monitorNewConnections) { // evaluate all the network interfaces that have connected since the // last snapshot foreach (NetworkInterface ni in current.Connected(_last)) { // test if the network interface was in the last snapshot: OperationalStatus lastStatus = OperationalStatus.NotPresent; if (_last.Contains(ni.Id)) { lastStatus = _last[ni.Id].OperationalStatus; } // raise the interface connected event: NetworkInterfaceConnected(this, new StatusMonitorEventArgs { EventType = StatusMonitorEventType.Connected, Interface = ni, LastOperationalStatus = lastStatus }); } } // test for interface dis-connections if (NetworkInterfaceDisconnected != null && _monitorDisconnections) { // enumerate the network interfaces that were Up but are not now. foreach (NetworkInterface ni in current.Disconnected(_last)) { // raise the interface dis-connected event: NetworkInterfaceDisconnected(this, new StatusMonitorEventArgs { // set the event-type, interface and last status. EventType = StatusMonitorEventType.Disconnected, Interface = ni, LastOperationalStatus = OperationalStatus.Up }); } } // test for interface changes. if (NetworkInterfaceChanged != null && _monitorAnyStatusChange) { // enumerate the interfaces that have changed status in any way since // the last snapshot. foreach (NetworkInterface ni in current.Changed(_last)) { // find the last status of the interface: OperationalStatus lastStatus = OperationalStatus.NotPresent; if (_last.Contains(ni.Id)) { lastStatus = _last[ni.Id].OperationalStatus; } // raise the interface changed event: NetworkInterfaceChanged(this, new StatusMonitorEventArgs { // set the event-type interface and last status. EventType = StatusMonitorEventType.Changed, Interface = ni, LastOperationalStatus = lastStatus }); } } if (NetworkAddressChanged != null && _monitorNetworkAddressChanged) { // enumerate the interfaces that have changed status in any way since // the last snapshot. foreach (NetworkInterface ni in current.AddressChange(_last)) { // find the last status of the interface: OperationalStatus lastStatus = OperationalStatus.NotPresent; if (_last.Contains(ni.Id)) { lastStatus = _last[ni.Id].OperationalStatus; } // raise the interface changed event: NetworkAddressChanged(this, new StatusMonitorEventArgs { // set the event-type interface and last status. EventType = StatusMonitorEventType.AddressChanged, Interface = ni, LastOperationalStatus = lastStatus }); } } // set last to the current. _last = current; // wait... if (_run) { Thread.Sleep(_waitInterval); } // pulse any threads waiting in WaitForPoll. lock (_pulse) Monitor.PulseAll(_pulse); } catch (Exception exception) { // handle the exception....(real exception handler should go here) Console.WriteLine(exception.ToString()); //// increment the exception counter. //Interlocked.Increment(ref _exceptionCount); } } }