// 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; }
/// <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); adapter.dlCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", name); adapter.ulCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name); this.adapters.Add(adapter); // Add it to ArrayList 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; }
private void MainFunction() { _adapters = null; _adapters = FoundNetworkAdapter(); if (_adapters.Length == 0) { MessageBox.Show("未在本机找到网络适配器!请检查硬件或者相应驱动是否正确安装"); } if (!File.Exists(Environment.CurrentDirectory + @"/settings.ini") || ConfigureFile.INIGetStringValue(Environment.CurrentDirectory + @"/settings.ini", "AdapterProperty", "SelectedAdapter", "$null") == "$null") { MessageBox.Show("您的电脑上存在多个网络适配器,请选择当前使用的适配器"); if (!File.Exists(Environment.CurrentDirectory + @"/settings.ini")) File.Create(Environment.CurrentDirectory + @"/settings.ini"); ConfigureFile.INIWriteItems(Environment.CurrentDirectory + @"/settings.ini", "AdapterProperty", "SelectedAdapter=$null"); SettingsWin sw = SettingsWin.GetInstance(); sw.Show(); _swTimer.Start(); } else { string selectedAdapter = ConfigureFile.INIGetStringValue( Environment.CurrentDirectory + @"/settings.ini", "AdapterProperty", "SelectedAdapter", "$null"); foreach (NetworkAdapter item in _adapters) { if (item.Name == selectedAdapter) { _adapterTemp = item; _monitor.StopMonitoring(); _monitor.StartMonitoring(item); _speedTimer.Start(); break; } } } }