2 Replies - 416 Views - Last Post: 12 October 2012 - 06:31 AM Rate Topic: -----

#1 Malasuerte94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-January 12

Why my downloading script stop after downloading first file?

Posted 12 October 2012 - 04:50 AM

Hi i have a bug in my updater, code here > The problem : after downloading first file scrip stop without downloading next file Where is the problem in scrip and how i can fix it ? Thanks for helping !
Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    Dim linkdl As String
    linkdl = "http://s3.amazonaws.com/MinecraftDownload/"
    Dim dirminecraft12 As String
    dirminecraft12 = (GetFolderPath(SpecialFolder.ApplicationData) & "/.minecraft/bin\")

    Dim filepaths As String()
    filepaths = {"minecraft.jar", "jinput.jar", "lwjgl.jar", "lwjgl_util.jar", "windows_natives.jar"}


    For Each filePath In filepaths
        'Creating the request and getting the response
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest

        theRequest = WebRequest.Create(linkdl & filePath)
        theResponse = theRequest.GetResponse

        Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)

        Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts2)
        Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
        Dim writeStream As New IO.FileStream(dirminecraft12 + filePath, IO.FileMode.Create)

        'Replacement for Stream.Position (webResponse stream doesn't support seek)
        Dim nRead As Integer

        'To calculate the download speed
        Dim speedtimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0

        Do

            speedtimer.Start()

            Dim readBytes(4095) As Byte
            Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)

            nRead += bytesread
            Dim percent As Integer = (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 'For increase precision, the speed it's calculated only every five cicles
                currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                speedtimer.Reset()
                readings = 0
            End If

        Loop
        'Close the streams
        theResponse.GetResponseStream.Close()
        writeStream.Close()

        Dim completeDelegate2 As New DownloadCompleteSafe(AddressOf DownloadComplete2)
        Me.Invoke(completeDelegate2, False)
    Next
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Why my downloading script stop after downloading first file?

#2 Malasuerte94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-January 12

Re: Why my downloading script stop after downloading first file?

Posted 12 October 2012 - 05:42 AM

Value of '2015' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'. Parameter name: Value here is error
Me.Invoke(safedelegate, length, nRead, percent, currentspeed)

Was This Post Helpful? 0
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Why my downloading script stop after downloading first file?

Posted 12 October 2012 - 06:31 AM

You calculate percent variable wrong.

length.......100%
nRead.......percent

percent = nRead * 100 / length


Edit:
sorry, that was stupid! There's nothing wrong with the way you are calculating, but is probably with the value of nRead, if it becomes > 100%
Edit 2: What I wanted to say is: nRead / length shouldn't be > 1

This post has been edited by lucky3: 12 October 2012 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1