public AdapterSelector(IEnumerable<NetworkAdapter> adapters) { InitializeComponent(); gridAdapters.ItemsSource = adapters; _adapterSelected = adapters.First(); }
// Enable the timer, and add the specified adapter to the monitoredAdapters list public void StartMonitoring(NetworkAdapter adapter) { if (!this.monitoredAdapters.Contains(adapter)) { this.monitoredAdapters.Add(adapter); adapter.init(); } timer.Enabled = true; }
private void gridAdapters_MouseDoubleClick_1(object sender, MouseButtonEventArgs e) { IInputElement element = e.MouseDevice.DirectlyOver; if (element != null && element is FrameworkElement) { if (((FrameworkElement)element).Parent is DataGridCell) { var grid = sender as DataGrid; if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1) { _adapterSelected = grid.SelectedItem as NetworkAdapter; ConfigHelper.AdapterName = _adapterSelected.Name; var win = this.Parent as Window; win.Close(); } } } }
void SetAdapterToMonitor() { _adapter = _adapters.FirstOrDefault(a => a.Name == ConfigHelper.AdapterName); try { monitor.StartMonitoring(_adapter); _timer.Elapsed += RefreshView; _timer.Start(); } catch (Exception) { MessageBox.Show("Not selected adapter."); } }
// Remove the specified adapter from the monitoredAdapters list, and disable the timer if the monitoredAdapters list is empty. public void StopMonitoring(NetworkAdapter adapter) { if (this.monitoredAdapters.Contains(adapter)) this.monitoredAdapters.Remove(adapter); if(this.monitoredAdapters.Count == 0) timer.Enabled = false; }
/// <summary> /// Enumerates network adapters installed on the computer. /// </summary> private void EnumerateNetworkAdapters() { PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface"); foreach (string name in category.GetInstanceNames()) { // This one exists on every computer. if (name == "MS TCP Loopback interface") continue; // Create an instance of NetworkAdapter class, and create performance counters for it. NetworkAdapter adapter = new NetworkAdapter(name, RefreshInterval); adapter.dlCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", name); adapter.ulCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name); if(!adapter.name.Contains("isatap.")) this.adapters.Add(adapter); // Add it to ArrayList adapter } }