/// <summary>
 /// Enable the timer, and add the specified adapter to the monitoredAdapters list
 /// </summary>
 public void StartMonitoring(NetWorkAdapter adapter)
 {
     if (!this.monitoredAdapters.Contains(adapter))
     {
         this.monitoredAdapters.Add(adapter);
         adapter.init();
     }
     timer.Enabled = true;
 }
Exemplo n.º 2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            NetWorkAdapter adapter = this.adapters[this.listBox1.SelectedIndex];

            /* The DownloadSpeedKbps and UploadSpeedKbps are double values. You can also
             * use properties DownloadSpeed and UploadSpeed, which are long values but
             * are measured in bytes per second. */
            this.label5.Text = String.Format("{0:n} kbps", adapter.DownloadSpeedKbps);
            this.label6.Text = String.Format("{0:n} kbps", adapter.UploadSpeedKbps);
        }
 /// <summary>
 /// Remove the specified adapter from the monitoredAdapters list, and
 /// disable the timer if the monitoredAdapters list is empty.
 /// </summary>
 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);
                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
            }
        }