1 Replies - 1669 Views - Last Post: 27 August 2013 - 01:24 PM Rate Topic: -----

#1 Innov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-August 13

Downloading multiple files, one at a time

Posted 23 August 2013 - 08:25 AM

I've created a vb.net form with multiple checkboxes and a download button, and wanted:
1) The ability to monitor the progress of individual downloads
2) Confirmation that a given download was cancelled or has completed.

I started by using My.Computer.Network.DownloadFile, but unfortunately, this option doesn't seem work for redirect URLS:
        Try
            My.Computer.Network.DownloadFile(url, sFileName, vbNullString, vbNullString, True, 5000, True, FileIO.UICancelOption.ThrowException)
        Catch ex As System.OperationCanceledException
            MessageBox.Show("Download Cancelled.")
            File.Delete(sFileName)
        End Try



I then looked at the option of WebClient:

....
		Using webClient = New WebClient()
            AddHandler webClient.DownloadFileCompleted, AddressOf Completed
            AddHandler webClient.DownloadProgressChanged, AddressOf ProgressChanged
            Form3.Label1.Text = programName
            sw.Start()
            webClient.DownloadFileAsync(New Uri(url), sFileName)
            While webClient.IsBusy
                Application.DoEvents()
            End While
        End Using
End Sub
		

    Private Sub ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
        Try
            Form3.Show()
            ' Calculate download speed and output it to label3
            If Form3.Label3.Text <> (Convert.ToDouble(e.BytesReceived) / 1024 / sw.Elapsed.TotalSeconds).ToString("0") Then
                Form3.Label3.Text = "Current Speed: " & (Convert.ToDouble(e.BytesReceived) / 1024 / sw.Elapsed.TotalSeconds).ToString("0.00") & " KB/s"
            End If

            ' Update the progressbar percentage only when the value is not the same
            If Form3.ProgressBar1.Value <> e.ProgressPercentage Then
                Form3.ProgressBar1.Value = e.ProgressPercentage
            End If

            ' Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
            Form3.Label2.Text = (Convert.ToDouble(e.BytesReceived) / 1048576).ToString("0.00") & " MB" & "  /  " & (Convert.ToDouble(e.TotalBytesToReceive) / 1024 \ 1024).ToString("0.00") & " MB"
            Form3.Label2.Text = "Downloading: " & (Convert.ToDouble(e.BytesReceived) / 1048576).ToString("0.00") & " MB" & " Of " & (Convert.ToDouble(e.TotalBytesToReceive) / 1048576).ToString("0.00") & " MB"

			Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
	
	Private Sub Completed(sender As Object, e As AsyncCompletedEventArgs)
        sw.Reset()
        If e.Cancelled = True Then
            MessageBox.Show("Download Cancelled")
        Else
            objectName.Checked = False
        End If
        Form3.Close()
    End Sub


This seems to work properly most of the time, but sometimes Label1 doesn't populate and the current speed shows as Infinity KB/s. When this does happen, I don't get confirmation that the download completes (even when it does). I have a feeling that the problem lies with the While webClient.IsBusy Application.DoEvents() portion, but I haven't yet been able to correct this.

Is This A Good Question/Topic? 0
  • +

Replies To: Downloading multiple files, one at a time

#2 Innov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-August 13

Re: Downloading multiple files, one at a time

Posted 27 August 2013 - 01:24 PM

^Bump^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1