private void UpdateNetworkInterfaces(ISettings settings)
        {
            // When multiple events fire concurrently, we don't want threads interfering
            // with others as they manipulate non-thread safe state.
            lock (_scanLock)
            {
                IOrderedEnumerable <NetworkInterface> networkInterfaces = GetNetworkInterfaces();
                if (networkInterfaces == null)
                {
                    return;
                }


                var foundNetworkInterfaces = networkInterfaces.ToDictionary(x => x.Id, x => x);

                // Remove network interfaces that no longer exist.
                List <string> removeKeys = new List <string>();
                foreach (KeyValuePair <string, Network> networkInterfacePair in _networks)
                {
                    if (foundNetworkInterfaces.ContainsKey(networkInterfacePair.Key))
                    {
                        continue;
                    }


                    removeKeys.Add(networkInterfacePair.Key);
                }

                foreach (string key in removeKeys)
                {
                    var network = _networks[key];
                    network.Dispose();
                    _networks.Remove(key);

                    _hardware.Remove(network);
                    HardwareRemoved?.Invoke(network);
                }

                // Add new network interfaces.
                foreach (KeyValuePair <string, NetworkInterface> networkInterfacePair in foundNetworkInterfaces)
                {
                    if (!_networks.ContainsKey(networkInterfacePair.Key))
                    {
                        _networks.Add(networkInterfacePair.Key, new Network(networkInterfacePair.Value, settings));
                        _hardware.Add(_networks[networkInterfacePair.Key]);
                        HardwareAdded?.Invoke(_networks[networkInterfacePair.Key]);
                    }
                }
            }
        }
Esempio n. 2
0
 private void HardwareAddedEvent(IHardware hardware)
 {
     HardwareAdded?.Invoke(hardware);
 }