5

For my application, I want to show the user about the network bandwidth. So, that getting delay for downloading will be known by the user.. Is it possible to show them??

Developer404
  • 5,716
  • 16
  • 64
  • 102
  • @RomanoZumbé that question is more than 7 years old. at that time, people won't look for an mcve to upvote. that is the initial phase. Please don't abuse those who are contributing from the starting stage of the site – Sagar V Jul 28 '17 at 07:58
  • 1
    @SagarV I'm not "abusing" anybody. This question popped up in my "Close Votes" review queue. I think if something doesn't meet our quality criteria today it should be deleted even if it met the quality criteria when it was posted. But I do understand now why it got upvoted in the first place. – Romano Zumbé Jul 28 '17 at 08:03

2 Answers2

4

Try this one: How to calculate network bandwidth speed in c#.

enter image description here

using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace InterfaceTrafficWatch
{
    /// <summary>
    /// Network Interface Traffic Watch
    /// by Mohamed Mansour
    /// 
    /// Free to use under GPL open source license!
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Timer Update (every 1 sec)
        /// </summary>
        private const double timerUpdate = 1000;

        /// <summary>
        /// Interface Storage
        /// </summary>
        private NetworkInterface[] nicArr;

        /// <summary>
        /// Main Timer Object 
        /// (we could use something more efficient such 
        /// as interop calls to HighPerformanceTimers)
        /// </summary>
        private Timer timer;

        /// <summary>
        /// Constructor
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            InitializeNetworkInterface();
            InitializeTimer();
        }

        /// <summary>
        /// Initialize all network interfaces on this computer
        /// </summary>
        private void InitializeNetworkInterface()
        {
            // Grab all local interfaces to this computer
            nicArr = NetworkInterface.GetAllNetworkInterfaces();

            // Add each interface name to the combo box
            for (int i = 0; i < nicArr.Length; i++)
                cmbInterface.Items.Add(nicArr[i].Name);

            // Change the initial selection to the first interface
            cmbInterface.SelectedIndex = 0;
        }

        /// <summary>
        /// Initialize the Timer
        /// </summary>
        private void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = (int)timerUpdate;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        /// <summary>
        /// Update GUI components for the network interfaces
        /// </summary>
        private void UpdateNetworkInterface()
        {
            // Grab NetworkInterface object that describes the current interface
            NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];

            // Grab the stats for that interface
            IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();

            // Calculate the speed of bytes going in and out
            // NOTE: we could use something faster and more reliable than Windows Forms Tiemr
            //       such as HighPerformanceTimer http://www.m0interactive.com/archives/2006/12/21/high_resolution_timer_in_net_2_0.html
            int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
            int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;

            // Update the labels
            lblSpeed.Text = nic.Speed.ToString();
            lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
            lblSpeed.Text = nic.Speed.ToString();
            lblBytesReceived.Text = interfaceStats.BytesReceived.ToString();
            lblBytesSent.Text = interfaceStats.BytesSent.ToString();
            lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
            lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";

        }

        /// <summary>
        /// The Timer event for each Tick (second) to update the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick(object sender, EventArgs e)
        {
            UpdateNetworkInterface();
        }

    }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Simon Linder
  • 3,378
  • 4
  • 32
  • 49
0

If it is that you're downloading via a stream then what you could do is to make a kind of "meter" which inherits from stream and exposes a property that is a summation of the bytes read or written to it's underlying stream object. Have the meter encapsulate the stream object that you're using to download and then read from the meter object. then ever so often you can check check the number of bytes read over time to calculate the consumed bandwidth speed.