none
DownloadFileAsync - Get Download Speed RRS feed

  • Question

  • Hello all, im trying to get current download speed.

    Im using DownloadFileAsync method to download files and this code to get current download speed:

            Dim SW As Stopwatch
    
            SW = Stopwatch.StartNew 'Put this before download start
    
            'KB
            Dim KB As Integer = e.BytesReceived / SW.ElapsedMilliseconds.ToString
            Dim KBtest = String.Format(Math.Round(KB, 2, MidpointRounding.AwayFromZero))
    
            'MB
            Dim MB As Integer = e.BytesReceived / SW.ElapsedMilliseconds.ToString / 1024
            Dim MBtest = String.Format(Math.Round(MB, 2, MidpointRounding.AwayFromZero))
    
            If MBtest < 1 Then
                TextBox1.Text = "Download speed = " & KBtest & " KB/s"
            Else
                TextBox1.Text = "Download speed = " & MBtest & " MB/s"
            End If

    Is this the best way to do it?

    The calculations seems to be not very good...

    Thanks

    Sunday, July 30, 2017 4:08 PM

Answers

  • Then using what you see above, the following does the actual calculations:

    Public ReadOnly Property ApproximateCompletionSeconds_String(Optional ByVal numberOfDecimalPlaces As Integer = 1) As String Get If _approximateCompletionSeconds.HasValue Then If numberOfDecimalPlaces < 0 Then numberOfDecimalPlaces = 1 End If Dim formatter As String = "f" & numberOfDecimalPlaces.ToString Return _approximateCompletionSeconds.Value.ToString(formatter) Else Return "--" End If End Get End Property Public ReadOnly Property ApproximateCompletionTime As String Get If _approximateCompletionSeconds.HasValue Then Dim dt As DateTime = Now.AddSeconds(_approximateCompletionSeconds.Value) If dt.Date = Today.Date Then Return dt.ToShortTimeString Else Return String.Format("{0} {1}", dt.ToShortDateString, dt.ToShortTimeString) End If Else Return "--" End If End Get End Property



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by extream87 Monday, July 31, 2017 1:05 PM
    Sunday, July 30, 2017 10:53 PM

All replies

  • Hello all, im trying to get current download speed.

    Im using DownloadFileAsync method to download files and this code to get current download speed:

            Dim SW As Stopwatch
    
            SW = Stopwatch.StartNew 'Put this before download start
    
            'KB
            Dim KB As Integer = e.BytesReceived / SW.ElapsedMilliseconds.ToString
            Dim KBtest = String.Format(Math.Round(KB, 2, MidpointRounding.AwayFromZero))
    
            'MB
            Dim MB As Integer = e.BytesReceived / SW.ElapsedMilliseconds.ToString / 1024
            Dim MBtest = String.Format(Math.Round(MB, 2, MidpointRounding.AwayFromZero))
    
            If MBtest < 1 Then
                TextBox1.Text = "Download speed = " & KBtest & " KB/s"
            Else
                TextBox1.Text = "Download speed = " & MBtest & " MB/s"
            End If

    Is this the best way to do it?

    The calculations seems to be not very good...

    Thanks

    It's just bytes per second, but you need to let it level out and you may need to use a buffered timer so that it's not jumping back and forth a lot.

    You never said if you want this thing I've been building but I'm continuing to add to it. I have the FTP namespace started and the HTTP namespace done, all but for testing and tweaking.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, July 30, 2017 4:19 PM
  • Thanks Frank
    What you recommend?
    Sunday, July 30, 2017 7:13 PM
  • Thanks Frank
    What you recommend?

    You mean for one single file being downloaded or for what I'm working on?

    With what I'm working on, I'm doing this for multiple files but of course, one file works fine too -- it's just a matter of what what properties of the EventArgs you show on your form. If you're downloading one file then "overall" and "current file" are the same. ;-)

    If you're doing this for one single file download, start a stopwatch and you can that and then use e.BytesReceived like you're doing. I'd suggest that you then use your stopwatch's .Elapsed property and after it's settled out (fully developed flow if you know fluid mechanics ;-)), display it; in the meantime, show something that let's them know it's being calculated or something like that.

    I realize that you're seeing the following out of context, but maybe it'll help some?

                Private Sub _
                    wc_DownloadProgressChanged(sender As Object, _
                                               e As System.Net.DownloadProgressChangedEventArgs)
    
                    _cumulativeTotalBytesDownloaded = _carryOverBytes + e.BytesReceived
                    _currentFileCumulativeBytes = e.BytesReceived
    
                    If _downloadSW IsNot Nothing AndAlso _downloadSW.IsRunning Then
                        _downloadRate = _cumulativeTotalBytesDownloaded / _downloadSW.Elapsed.TotalSeconds
                    End If
    
                End Sub

    The variable "_downloadRate" is what I'm talking about.


    *****

    In mine, do understand that it's a library; an assembly.

    I'll explain more about that if you're interested?


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, July 30, 2017 7:31 PM
  • Thanks :D
    Now im trying to get download timeleft and i've tried:

    Dim timeleft As Double = (e.TotalBytesToReceive - e.BytesReceived) * SW.Elapsed.ToString / e.BytesReceived

    But i get error: Additional information: The "00: 00: 00.3415417" string conversion to type 'Double' is not valid.

    Sunday, July 30, 2017 9:49 PM
  • Thanks :D
    Now im trying to get download timeleft and i've tried:

    Dim timeleft As Double = (e.TotalBytesToReceive - e.BytesReceived) * SW.Elapsed.ToString / e.BytesReceived

    But i get error: Additional information: The "00: 00: 00.3415417" string conversion to type 'Double' is not valid.

    The following is from the FTP side, so the variables are similar but not the same as I posted earlier.

    I have a timer that *ticks* every four seconds you'll see shown here (the buffered timer I talked about earlier):

                Private Sub _
                  bufferredTimer_Tick(sender As Object, _
                                      e As System.EventArgs) _
                                      Handles bufferredTimer.Tick
    
                    If _transferSW IsNot Nothing AndAlso _transferSW.IsRunning Then
                        If _transferSW.Elapsed.TotalSeconds > 0 Then
                            _transferRate = (_cumulativeBytesTransferred - _overallResidualBytes) / _transferSW.Elapsed.TotalSeconds
    
                            If _transferRate > 0 Then
                                _approximateCompletionSeconds = (_filesBytesTotal - _cumulativeBytesTransferred) / _transferRate
                            End If
                        End If
                    End If
    
                End Sub

    ***** I removed the earlier post - this one is more complete *****


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, July 30, 2017 10:30 PM
  • Then using what you see above, the following does the actual calculations:

    Public ReadOnly Property ApproximateCompletionSeconds_String(Optional ByVal numberOfDecimalPlaces As Integer = 1) As String Get If _approximateCompletionSeconds.HasValue Then If numberOfDecimalPlaces < 0 Then numberOfDecimalPlaces = 1 End If Dim formatter As String = "f" & numberOfDecimalPlaces.ToString Return _approximateCompletionSeconds.Value.ToString(formatter) Else Return "--" End If End Get End Property Public ReadOnly Property ApproximateCompletionTime As String Get If _approximateCompletionSeconds.HasValue Then Dim dt As DateTime = Now.AddSeconds(_approximateCompletionSeconds.Value) If dt.Date = Today.Date Then Return dt.ToShortTimeString Else Return String.Format("{0} {1}", dt.ToShortDateString, dt.ToShortTimeString) End If Else Return "--" End If End Get End Property



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by extream87 Monday, July 31, 2017 1:05 PM
    Sunday, July 30, 2017 10:53 PM