Get network bandwidth information
<p>I use a timer to get bandwidth per second and place that information on a label The output is a large value like <code>419000KB/S</code> and next tick it may be <code>0KB/S</code>. Is it a problem related to thread or something? I guess it was <code>bytes</code> can not update properly, but I have no idea to fix it.</p>

<pre><code>    public partial class MainWindow : Window
    {
        static long bytes = 0;
        static NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
        static Label uploadLabel;
        public MainWindow()
        {
            InitializeComponent();
            uploadLabel = uploadBandwidth;
            IPv4InterfaceStatistics statistics = adapters[0].GetIPv4Statistics();

            bytes = statistics.BytesSent;
            Timer myTimer = new Timer();
            myTimer.Elapsed += new ElapsedEventHandler(TimeUp);
            myTimer.Interval = 1000;
            myTimer.Start();
        }

        public void TimeUp(object source, ElapsedEventArgs e)
        {
            IPv4InterfaceStatistics statistics = adapters[0].GetIPv4Statistics();
            bytes = statistics.BytesSent - bytes;
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action&lt;Label&gt;(SetValue), uploadLabel);
        }

        private void SetValue(Label upload)
        {
            upload.Content = ((int)bytes / 1024).ToString() + "KB/s";
        }
    }
</code></pre>

<p>Update:</p>

<p>The problem is I change the value <code>bytes</code> to <code>statistics.BytesSent - bytes</code> which is wrong.
Here is the function I modified:</p>

<pre><code>public void TimeUp(object source, ElapsedEventArgs e)
{
    IPv4InterfaceStatistics statistics = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();
    long bytesPerSec = statistics.BytesReceived - bytes;
    bytes = statistics.BytesReceived;
    String speed = (bytesPerSec / 1024).ToString() + "KB/S";
    Dispatcher.Invoke(DispatcherPriority.Normal, new Action&lt;Label, String&gt;(SetValue), uploadLabel, speed);
}

private void SetValue(Label upload, String speed)
{
    upload.Content = speed;
}
</code></pre>