Results 1 to 8 of 8

Thread: Calculate Download time and stop a download ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    9

    Calculate Download time and stop a download ?

    Hello guys,

    Iam working on a Download Manager, but I dont know the codes for calculating the download time and how to stop ( not cancel) the download and then continue it. Perhaps the codes for the Download-speed,percentage and size could help you.
    Code:
     Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
    
            Me.lblsize.Text = "Size : " & Math.Round((length / 1024), 2) & " KB"
            Me.lblstat.Text = "Status : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
            Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
            If speed = -1 Then
                Me.lblspd.Text = "Speed : " & "calculating..."
            Else
                Me.lblspd.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
            End If
    
            Me.ProgressBar1.Value = percent
        End Sub
    I hope you can help me.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Calculate Download time and stop a download ?

    Are you seeing unexpected results from your code? Are you using all the same units? Remember, kbps <> kBps.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    9

    Re: Calculate Download time and stop a download ?

    Im using this Download Manager http://www.mediafire.com/download.php?mhuktzznl2m, please have a look so it will be easier for you to help me because its very difficult for me to explain things ( Im from Germany )

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Calculate Download time and stop a download ?

    I am very skeptical to download code from someone else. Your first post was explained quite well, why not give it a shot?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    9

    Re: Calculate Download time and stop a download ?

    I thought it would be a help for you if you saw the whole code for my Downloader (The Code written is by Carmine_XX ) because than you will have a better chance to help me. I searched on the internet and didnt found anything, so I have to ask in Forums where people could have the right answer. Perhaps you have some ideas or tips how to solve my problem, it would be great.
    Last edited by Flow96; Jun 8th, 2011 at 11:48 AM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    9

    Re: Calculate Download time and stop a download ?

    So minutes ago I found the Code how to calculate the Download Time in a German forum http://www.vb-paradise.de/programmie...ftp-ermitteln/ wriiten by the the user 4typen :
    1. Timer (Timer1) -> Change interval to "1000".
    2. Declare :
    Code:
    Dim sek As Integer = 0Dim kb As Double
    2. Connect Progressbar to the Uploadprogress (Link)
    4. Code for the Timer1:
    Code:
     Try
    sek += 1
    If sek > 3 Then
    Dim zeitproeinheit As Double = sek / ProgressBar1.ProgressValue
    Dim nochzumachen As Integer = 100 - ProgressBar1.ProgressValue
    Dim min As Long = 0
    Dim zeit As Integer = nochzumachen * zeitproeinheit
    If zeit > 60 Then
    Do Until zeit < 60
    zeit -= 60
    min += 1
    Loop
    End If
    If zeit < 10 Then
    Label1.Text = "Noch " & min & ":0" & zeit & " (" & Math.Round(kb, 0) & " KB/s)"
    Else
    Label1.Text = "Noch " & min & ":" & zeit & " (" & Math.Round(kb, 0) & " KB/s)"
    End If
    Else
    Label1.Text = "Calculate time..."
    End If
    Catch
    End Try
    So one problem is solved, but I also need help with the Code for stopping the download and continue it after a while.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Calculate Download time and stop a download ?

    It is probably better to post that question is a new thread. Also, how are you downloading?

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    9

    Re: Calculate Download time and stop a download ?

    Here is the code of the Form, so you can see how it works :
    Code:
    Imports System.Net
    Public Class Form5
        Dim whereToSave As String
        Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
        Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
        Dim sek As Integer = 0
        Dim kb As Double
    
        Public Sub DownloadComplete(ByVal cancelled As Boolean)
            Me.txtfilename.Enabled = True
    
            If cancelled Then
    
                Me.lblstat.Text = "Download Abgebrochen"
                MessageBox.Show("Der Download wurde abgebrochen !", "Abgebrochen", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Text = "0%" + " " + "von " & Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1) & " - Download abgebrochen"
            Else
                Me.lblstat.Text = "Erfolgreich gedownloadet"
                Me.Text = "100%" + " " + "von " & Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1) & " - Download abgeschlossen"
                Erfolgreich()
    
            End If
            Me.ProgressBar1.Value = 0
        End Sub
        Private Sub Form5_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            BackgroundWorker1.WorkerSupportsCancellation = True
            BackgroundWorker1.CancelAsync()
        End Sub
        Private Sub Erfolgreich()
            If RadioButton1.Checked = True And MessageBox.Show("Der Download wurde erfolgreich abgeschlossen !", "Erfolgreicher Download", MessageBoxButtons.OK, MessageBoxIcon.Information) = MsgBoxResult.Ok Then
                Process.Start(whereToSave)
                Me.Close()
                Form1.Show()
            Else
    
            End If
        End Sub
        Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal time As Integer)
            Me.lblstat.Text = Label1.Text & " - " & Math.Round((position / 1048576), 2) & " MB von " & Math.Round((length / 1048576), 2) & "MB (" & Me.ProgressBar1.Value & "%)"
            Me.Text = Me.ProgressBar1.Value & "%" + " " + "von " & Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1) & " - Downloads"
            Downloadzeit()
            Me.ProgressBar1.Value = percent
        End Sub
    
    
        Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.Text = Me.ProgressBar1.Value & "%" + "von " & Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1) & " - Downloads"
            Me.txtfilename.Text = Form1.txtfilename5.Text
            Me.SaveFileDialog1.FileName = Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1)
            Me.lblname.Text = Form1.txtfilename5.Text.Split("/"c)(Form1.txtfilename5.Text.Split("/"c).Length - 1)
            Me.SaveFileDialog1.ShowDialog()
            Me.loc.Text = Me.SaveFileDialog1.FileName
            Downloadfile()
        End Sub
        Private Sub Downloadfile()
    
            If Me.txtfilename.Text <> "" AndAlso Me.txtfilename.Text.StartsWith("http://") Then
                Me.whereToSave = Me.loc.Text
                Me.SaveFileDialog1.FileName = ""
                Me.txtfilename.Enabled = False
                Me.loc.Enabled = False
                Me.BackgroundWorker1.RunWorkerAsync()
            Else
    
                MessageBox.Show("Der Download ist nicht verfügbar", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End Sub
        Private Sub Downloadzeit()
            Timer1.Start()
        End Sub
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim theResponse As HttpWebResponse
            Dim theRequest As HttpWebRequest
            Try
                theRequest = WebRequest.Create(Me.txtfilename.Text)
                theResponse = theRequest.GetResponse
            Catch ex As Exception
                MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
                                "1) File doesn't exist" & ControlChars.CrLf & _
                                "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
                Me.Invoke(cancelDelegate, True)
                Exit Sub
            End Try
            Dim length As Long = theResponse.ContentLength
            Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
            Me.Invoke(safedelegate, length, 0, 0, 0)
            Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
            Dim nRead As Integer
            Dim speedtimer As New Stopwatch
            Dim currentspeed As Double = -1
            Dim readings As Integer = 0
    
            Do
                If BackgroundWorker1.CancellationPending Then
                    Exit Do
                End If
                speedtimer.Start()
                Dim readBytes(4095) As Byte
                Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
                nRead += bytesread
                Dim percent As Short = (nRead / length) * 100
                Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
    
                If bytesread = 0 Then Exit Do
                writeStream.Write(readBytes, 0, bytesread)
                speedtimer.Stop()
                readings += 1
    
                If readings >= 5 Then
                    currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                    speedtimer.Reset()
                    readings = 0
                End If
            Loop
    
            theResponse.GetResponseStream.Close()
            writeStream.Close()
    
            If Me.BackgroundWorker1.CancellationPending Then
                IO.File.Delete(Me.whereToSave)
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
                Me.Invoke(cancelDelegate, True)
                Exit Sub
            End If
    
            Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Me.Invoke(completeDelegate, False)
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
            BackgroundWorker1.WorkerSupportsCancellation = True
            BackgroundWorker1.CancelAsync()
            PictureBox1.Visible = False
            PictureBox3.Visible = True
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.WorkerSupportsCancellation = True
            BackgroundWorker1.CancelAsync()
            Me.Close()
        End Sub
    
        Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Try
                sek += 1
                If sek > 3 Then
                    Dim zeitproeinheit As Double = sek / ProgressBar1.Value
                    Dim nochzumachen As Integer = 100 - ProgressBar1.Value
                    Dim min As Long = 0
                    Dim zeit As Integer = nochzumachen * zeitproeinheit
                    If zeit > 60 Then
                        Do Until zeit < 60
                            zeit -= 60
                            min += 1
                        Loop
                    End If
                    If zeit < 10 Then
                        Label1.Text = min & " Minuten , " & zeit & " Sekunden verbleiben"
                    Else
                        Label1.Text = min & " Minuten , " & zeit & " Sekunden verbleiben"
                    End If
                Else
                    Label1.Text = "Restzeit wird berechnet..."
                End If
            Catch
            End Try
        End Sub
    End Class

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
  •  



Click Here to Expand Forum to Full Width