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