2014年3月8日 星期六

vbnet Download File Asynchronously With ProgressBar (正確沒問題版)

參考引用
--
  1. Private Sub btnStartDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownload.Click
  2. Dim client As WebClient = New WebClient
  3. AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
  4. AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
  5. client.DownloadFileAsync(New Uri("SomeURL"), "SomePlaceOnHardDrive")
  6. btnStartDownload.Text = "Download in Progress"
  7. btnStartDownload.Enabled = False
  8. End Sub

  1. Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
  2. Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
  3. Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
  4. Dim percentage As Double = bytesIn / totalBytes * 100
  5.  
  6. progressBar.Value = Int32.Parse(Math.Truncate(percentage).ToString())
  7. End Sub

  1. Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
  2. MessageBox.Show("Download Complete")
  3. btnStartDownload.Text = "Start Download"
  4. btnStartDownload.Enabled = True
  5. End Sub

沒有留言:

張貼留言