5 Replies - 1154 Views - Last Post: 16 February 2013 - 10:37 PM Rate Topic: -----

#1 OmitZz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 12

Getting download speed and percentage

Posted 15 February 2013 - 03:03 PM

I am working on making my programs updater better by letting you know the download speed and the percentage of the downloaded file but I am running into problems. I am trying to get the download speed by running a timer on an interval of 1 second and every second it compares the current kilobytes download minus the kilobytes download last time the timer ticked so it would be how many kilobytes have been download in that second and as for the percentage I divide the progressbar minimum which is set to the currently downloaded file size by the progressbar maximum which is the total file size and multiply that by 100 and display that in the disabled button text. Every time it ticks, it gives me back nothing for the current file size so the speed simply becomes the current download speed because it isn't setting the old downloaded size so it becomes 0 and the percentage becomes 0 as well. I am sure there is something wrong with this somewhere because it's not working but I cannot seem to find it! I have been stumped for hours now trying different methods of displaying the same values and stuff. Here is my full code.

Private Sub Updater_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Text = Me.ProductVersion
        Try
            Dim instance As WebClient = New WebClient
            Dim address As String = "https://dl.dropbox.com/u/76035776/xPro/Version.txt"
            Dim returnValue As String
            returnValue = instance.DownloadString(address)
            Label4.Text = returnValue
            If Label3.Text >= Label4.Text Then
            Else
                Button1.Enabled = True
            End If
        Catch ex As Exception

        End Try
        Control.CheckForIllegalCrossThreadCalls = False
        Try
            Dim instance As WebClient = New WebClient
            Dim address As String = "https://dl.dropbox.com/u/76035776/xPro/UpdateNews.txt"
            Dim returnValue As String
            returnValue = instance.DownloadString(address)
            TextBox1.Text = returnValue
        Catch ex As Exception

        End Try
    End Sub
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Try
            Shell(MyDirectory + "\xPro.exe")
            Form1.Close()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        My.Computer.FileSystem.RenameFile(MyDirectory + "\xPro.exe", "xPro.old")
        Timer1.Start()
        BackgroundWorker1.RunWorkerAsync()
        Button1.Enabled = False
    End Sub
    Public totalsize As String
    Public link As String
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try

            link = "https://dl.dropbox.com/u/76035776/xPro/xPro.exe"
            Dim size1 As Integer
            Dim wr As WebRequest
            wr = WebRequest.Create(link)
            Dim webr As WebResponse = wr.GetResponse
            size1 = webr.ContentLength
            webr.Close()
            size1 = size1 / 1024
            ProgressBar1.Maximum = size1
            My.Computer.Network.DownloadFile("https://dl.dropbox.com/u/76035776/xPro/xPro.exe", MyDirectory + "\xPro.exe")
        Catch ex As Exception

        End Try
    End Sub
    Public Csize As String
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim amount As Integer
        Dim oldspeed As Integer
        If File.Exists(MyDirectory + "\xPro.exe") Then
            Dim o As New FileInfo(MyDirectory + "\xPro.exe")
            Try
                oldspeed = amount
            Catch ex As Exception

            End Try
            amount = o.Length
            amount = amount / 1024
            Csize = amount
            ProgressBar1.Value = amount
        End If
        Try
            Dim newspeed As Integer
            newspeed = amount - oldspeed
            Label8.Text = newspeed.ToString
        Catch ex As Exception

        End Try
        If Not Csize = Nothing Then
            Label7.Text = Csize + " of " + totalsize + " KB"
        End If
        Dim percentdone As Integer
        percentdone.Equals(ProgressBar1.Value / ProgressBar1.Maximum * 100)
        Button1.Text = percentdone.ToString + "%"
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Getting download speed and percentage

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Getting download speed and percentage

Posted 15 February 2013 - 04:02 PM

The DownloadProgressChanged tells these values inside the event e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage and you don't have to worry about cross thread calls here.
Was This Post Helpful? 0
  • +
  • -

#3 OmitZz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 12

Re: Getting download speed and percentage

Posted 16 February 2013 - 06:44 PM

View Post_HAWK_, on 15 February 2013 - 04:02 PM, said:

The DownloadProgressChanged tells these values inside the event e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage and you don't have to worry about cross thread calls here.


I tried e.progresspercentage but that did nothing. I still don't understand how the value keeps coming back 0.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Getting download speed and percentage

Posted 16 February 2013 - 06:50 PM

Did you add that event, and how did you add it?
Was This Post Helpful? 0
  • +
  • -

#5 OmitZz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 04-May 12

Re: Getting download speed and percentage

Posted 16 February 2013 - 10:20 PM

View Post_HAWK_, on 16 February 2013 - 06:50 PM, said:

Did you add that event, and how did you add it?

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Button1.Text = e.ProgressPercentage
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Getting download speed and percentage

Posted 16 February 2013 - 10:37 PM

First we are talking about a webClient and now your talking about a BGW??? The WebClient has a DownloadProgressChanged event and you were supposed to use that.

'where you create the WebClient
Addhandler instance.DownloadProgressChanged, AddressOf progress

'is routed to this sub
Private Sub progress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
  'here you get the e.ProgressPercentage
End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1