|
-
Feb 5th, 2011, 04:00 PM
#1
Thread Starter
Lively Member
[RESOLVED] Download: Calculating speed and time left (progress bar)
Hi,
I created a software which download files from the web.
I also added a progress bar which shows the download progress.
Every thing works fine.
Now:
- How can I add a lable that will calculate the time left for the file to be downloaded and another label that will tell the speed of the download?
- Would it also be possible to add a lable which calculates the size of the file that is beign downloaded?
Here is what I declared:
Imports System.Net
Public Class Form1
Dim WithEvents Download As New WebClient
Dim WithEvents Download1 As New WebClient
Friend WithEvents Label1 As System.Windows.Forms.Label
Here is what I have under the download button:
Download.DownloadFileAsync(New Uri("URL" & ComboBox1.Text & ".rar"), Textbox1.Text & "\" & ComboBox1.Text & ".rar")
And here is what I have under Download_DownloadProgressChanged:
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = e.ProgressPercentage & "%"
If ProgressBar1.Value = 100 Then
MsgBox("Download Completed!", vbInformation, "Info")
Label1.Text = "0%"
Thanks in advance for your help
Andrea
-
Feb 5th, 2011, 04:49 PM
#2
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Anyone?
I tried to make something up but eventhough it seems working, I do not think that the results showed are correct.
Here is what i tried to do:
' Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Label4.Text = Label4.Text + 1 'second counter whic is hidden in the form
'End Sub
'Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
'Dim SpeedFormula As Single = (ProgressBar1.Value / Label4.Text)
'Dim SpeedOperationK As Single = SpeedFormula * 1000
'Dim SpeedOperationM As Single = SpeedFormula
'Dim TimeFormula As Single = ((100 - ProgressBar1.Value) / ((ProgressBar1.Value / Label4.Text) * 1000))
'Dim TimeOperationS As Single = TimeFormula * 1000
'Dim TimeOperationM As Single = (TimeFormula * 1000) * 60
'Dim TimeOperationH As Single = (TimeFormula * 1000) * 3600
' If SpeedOperationK < 1000 Then
' Label5.Text = "Download Speed: " & Mid(SpeedOperationK, 1, 3) & " KB/sec"
' ElseIf SpeedOperationK > 999 Then
' Label5.Text = "Download Speed: " & Mid(SpeedOperationM, 1, 3) & " MB/sec"
' End If
' If TimeOperationS < 61 Then
' Label6.Text = "Time Left: " & Mid(TimeOperationS, 1, 1) & " seconds"
' ElseIf TimeOperationS > 60 Then
' Label6.Text = "Time Left: " & Mid(TimeOperationM, 1, 1) & " min and " & Mid(TimeOperationM, 2, 1) & " sec"
' ElseIf TimeOperationS > 3599 Then
' Label6.Text = "Time Left: " & Mid(TimeOperationH, 1, 1) & "h and " & Mid(TimeOperationH, 2, 1) & " min"
' End If
' End Sub
Anyone? Please...
Thank you
-
Feb 5th, 2011, 07:40 PM
#3
Re: Download: Calculating speed and time left (progress bar)
I think this should work:
Code:
TimeRemaining = (100- downloaded) * TimeElapsed/downloaded
So for e.g. suppose 10% of the file has been downloaded in 5 minutes then
Code:
TimeRemaining = (100 - 10) * 5/10
= 45 minutes
-
Feb 5th, 2011, 08:27 PM
#4
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
thanks for your answer. But how would I calculate "downloaded" which I suppose is the ammount of bytes already downloaded?
-
Feb 5th, 2011, 08:29 PM
#5
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
UH I got it now! for download you mean the current progressbar value. mmm I do not think it will work... but I will try right now and I will let you know.
Thank you,
Andrea
Last edited by Netmaster; Feb 5th, 2011 at 08:35 PM.
-
Feb 5th, 2011, 08:32 PM
#6
Re: Download: Calculating speed and time left (progress bar)
NO, that's the % of file downloaded. (see example)
If you know the filesize then you can also use,
Code:
TimeRemaining = (TotalFileSize - BytesDownloaded) * TimeElapsed/BytesDownloaded
EDIT:
OK I see you already figured out. This reply was actually in response to post #4
Last edited by Pradeep1210; Feb 5th, 2011 at 08:35 PM.
Reason: clarification
-
Feb 5th, 2011, 09:46 PM
#7
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Thanks a lot Pradeep it works! (TimeRemaining = (100- downloaded) * TimeElapsed/downloaded)
Here is what I exactly did:
Code:
Private Sub Download_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles Download.DownloadProgressChanged
Dim FormulaH As Single = ((100 - ProgressBar1.Value) * Time / ProgressBar1.Value) / 3600
Dim FormulaM As Single = ((100 - ProgressBar1.Value) * Time / ProgressBar1.Value) / 60
Dim FormulaS As Single = (100 - ProgressBar1.Value) * Time / ProgressBar1.Value
If ProgressBar1.Value = 0 Then
Label6.Text = "Time Left: Estimating..."
Else
If FormulaS > 3599 Then
Label6.Text = "Time Left: " & Mid(FormulaH, 1, 2) & " h and " & Mid(FormulaM, 1, 2) & " min"
ElseIf FormulaS > 60 And FormulaS < 3600 Then
Label6.Text = "Time Left: " & Mid(FormulaM, 1, 2) & " min and " & Mid(FormulaS, 1, 2) & " sec"
Else
Label6.Text = "Time Left: " & Mid(FormulaS, 1, 2) & " sec"
End If
End If
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = e.ProgressPercentage & "%"
End Sub
Where "Time" is just the timer which counts seconds.
Now... the only little problem is that sometimes the estimate time will state something like:
Time left: "11 min and 65 sec"
How could I fix it with:
"Time left: 12 min and 5 sec" ?
Thanks a lot again!
-
Feb 5th, 2011, 10:47 PM
#8
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
I think I figured out how to calculate the download speed now, it seems working:
Code:
Dim Speed As Single = (100 - ProgressBar1.Value) / ((100 - ProgressBar1.Value) * Time / ProgressBar1.Value)
If Speed < 1 Then
Label5.Text = "Download Speed: " & Mid(Speed, 3, 3) & " KB/s"
Else
Label5.Text = "Download Speed: " & Mid(Speed, 1, 3) & " MB/s"
End If
The problem is that everytime an integer is = to 0, the 0 will not be displayed!
For example if the download speed is 200 kb/s the label5.text will show "Download speed: 2 kb/s" and not "Download speed: 200 kb/s".
The same thing if for example the download speed is 190 kb/s, the label5.text will only show "19 kb/s"
-------
So basically now the things to fix are two:
- TIME LEFT (example: "Time left: 12 min and 5 sec" instead of "Time left: 11 min and 65 sec")
- DOWNLOAD SPEED when second and third integer are "0"
Hope you can help.
Thanks a lot,
Andrea
-
Feb 6th, 2011, 03:27 AM
#9
Re: Download: Calculating speed and time left (progress bar)
It's easier:
Code:
Speed = downloaded/Time
-
Feb 6th, 2011, 03:54 AM
#10
Re: Download: Calculating speed and time left (progress bar)
 Originally Posted by Netmaster
Thanks a lot Pradeep it works! (TimeRemaining = (100- downloaded) * TimeElapsed/downloaded)
Here is what I exactly did:
...
Where "Time" is just the timer which counts seconds.
Now... the only little problem is that sometimes the estimate time will state something like:
Time left: "11 min and 65 sec"
How could I fix it with:
"Time left: 12 min and 5 sec" ?
Thanks a lot again!
vb.net Code:
If ProgressBar1.Value = 0 Then Label6.Text = "Time Left: Estimating..." Else Dim secondsRemaining As Double = (100 - ProgressBar1.Value) * time / ProgressBar1.Value Dim ts As TimeSpan = TimeSpan.FromSeconds(secondsRemaining) Label6.Text = String.Format("Time Left: {0}:{1}:{2}", ts.Hours, ts.Minutes, ts.Seconds) Select Case secondsRemaining Case Is < 60 Label6.Text = String.Format("Time Left: {0} sec", ts.Seconds) Case Is < 3600 Label6.Text = String.Format("Time Left: {0} min {1} sec", ts.Minutes, ts.Seconds) Case Is < 86400 Label6.Text = String.Format("Time Left: {0} hours {1} min {2} sec", ts.Hours, ts.Minutes, ts.Seconds) Case Else Label6.Text = String.Format("Time Left: {0} days {1} hours {2} min {3} sec", ts.Days, ts.Hours, ts.Minutes, ts.Seconds) End Select End If
Last edited by Pradeep1210; Feb 6th, 2011 at 04:02 AM.
-
Feb 6th, 2011, 04:26 AM
#11
Re: Download: Calculating speed and time left (progress bar)
You won't be able to get speed in KB/s or MB/s because you are not calcualting in bytes.
If you have the downloaded bytes in some variable it is easy to calculate
vb.net Code:
Dim bytesDownloaded As Long = 123456 '<-- replace 123456 with actual variable that holds bytes downloaded Dim kbps As Double = bytesDownloaded / (time * 1024) If kbps < 1024 Then Label5.Text = String.Format("Speed: {0:0.00} KB/s", kbps) Else Label5.Text = String.Format("Speed: {0:0.00} MB/s", kbps / 1024) End If
Otherwise you can use the progressbar value, but the value is not in KB/s or MB/s. So don't put that after the value and it should give you some estimation.
vb.net Code:
Label5.Text = String.Format("Speed: {0:0.00}", ProgressBar1.Value / time)
-
Feb 6th, 2011, 11:08 AM
#12
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Wow Thanks! you are great!
The time now works great now!
About the speed:
Is it easy to store the downloaded bytes in a variable so I can also calulate the speed of the download?
Thank youuuuuu
-
Feb 6th, 2011, 02:51 PM
#13
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Hi pradeep,
I tried to do a thing today.
A created a new project to see if I could get the bytes of a downloading files and returning the download speed. What I did was:
I created a form with:
-Button1
-Label1
-Label2
-TextBox1
-Tmer1
Here is the code:
Code:
Public Class Form1
Dim time As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
Dim file As String = TextBox1.Text 'URL of the file to be downloaded
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(file)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As System.IO.Stream = response.GetResponseStream()
' Get the length of the content
Dim length As Integer = response.ContentLength
' Create a temporary array for the content of the file.
Dim bytes(length) As Byte
For i As Integer = 0 To length - 1
Dim bytesDownloaded As Long = i.ToString
bytes(i) = stream.ReadByte()
Label1.Text = bytesDownloaded & "Bytes Downloaded"
Dim kbps As Double = (bytesDownloaded / (time * 1024))
If kbps < 1024 Then
Label2.Text = String.Format("Speed: {0:0.00} KB/s", kbps)
Else
Label2.Text = String.Format("Speed: {0:0.00} MB/s", kbps / 1024)
End If
Application.DoEvents()
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time = Time + 1
End Sub
It works well beside the fact that label2 returns the speed in the format, for example, 1.12 kb/s (instead of 112kb/s).
Now, I just cannot figure out a way to implet this in my project.
I tried different ways but I did not get it to work.
Do you have any idea on how to do that?
Thank you,
Andrea
-
Feb 6th, 2011, 05:49 PM
#14
Re: Download: Calculating speed and time left (progress bar)
Remove that 1024. It was to convert bytes to kilobytes. But from what you are looking for, it looks like your file size is already in kilobytes.
Code:
Dim kbps As Double = bytesDownloaded / time
-
Feb 6th, 2011, 05:56 PM
#15
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
the last code i wrote was for an otehr pogram. it won/t work on the project i am working on since in this project i am using DownloadFileAsync. =(
any idea on how to integrate the download bytes calculation in my program?
thank you
a.
-
Feb 6th, 2011, 08:59 PM
#16
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Pradeep1210 I did it!
What I did to calculate the bytes of the file was just checking the file size on the directory it was located on the pc! That did the trick!
Code:
Dim DownloadFile As File
DownloadFile = fso.GetFile(Textbox1.Text & "\" & ComboBox1.Text & ".rar")
Dim bytesDownloaded As Long = DownloadFile.Size
Dim kbps As Double = bytesDownloaded / (Time * 1024)
If kbps < 1024 Then
Label5.Text = String.Format("Download Speed: {0:0.00} KB/s", kbps)
Else
Label5.Text = String.Format("Download Speed: {0:0.00} MB/s", kbps / 1024)
End If
Yeyy Thanks a lot!
Could you please just tell me one more thing?
If the download speed is over 1mb/s is fine: the speed showed is, for exampe, 1.24 MB/s
But if the download speed is in Kb/s I get, for example, this: 330.33 KB/s instead of 330.
How can i remove the last 2 digits? I tried to use the Math.round but it did not work.
Thank you agaiiin,
A.
EDIT:
I think I did it:
Code:
If kbps < 1024 Then
Label5.Text = String.Format("Download Speed: {0:0.00} KB/s", (Mid(kbps, 1, 3)))
I just added (Mid(kbps, 1, 3)) which works fine if the download is > 99. But wat about if it is < 100kb/s?
What would it show? some thing like 90 kb/s or .90 kb/s?
I cant find a slow download to test it
EDIT2:
Yes it gives .99 and i belive if the download is under 10kb/s it will give ..9.
Is there any way to get rid of the "."?
Thanks
A.
Last edited by Netmaster; Feb 6th, 2011 at 09:20 PM.
-
Feb 7th, 2011, 02:28 AM
#17
Re: Download: Calculating speed and time left (progress bar)
Try this:
vb.net Code:
Dim fi As New IO.FileInfo(TextBox1.Text & "\" & ComboBox1.Text & ".rar") Dim kbDownloaded As Long = fi.Length \ 1024 Dim kbps As Long = kbDownloaded \ Time If kbps < 1024 Then Label5.Text = String.Format("Download Speed: {0} KB/s", kbps) Else Label5.Text = String.Format("Download Speed: {0} MB/s", kbps \ 1024) End If
-
Feb 7th, 2011, 10:21 AM
#18
Thread Starter
Lively Member
Re: Download: Calculating speed and time left (progress bar)
Pradeep1210.... You are great! Thanks a lot! I will mark the tread as solved!
Would you mind taking a look at another thread I posted? I have a really hard time trying t o figure out how to do a thing.
Here is the link in case you wanna check it out:
http://www.vbforums.com/showthread.p...85#post3956485
Thank you a lot again,
Andrea
EDIT: I solved that problem as well! Again, thanks a lot for your help
=D
Last edited by Netmaster; Feb 7th, 2011 at 02:50 PM.
-
Jan 8th, 2013, 05:44 PM
#19
New Member
Re: [RESOLVED] Download: Calculating speed and time left (progress bar)
could you please paste full code , thanx
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|