BackgroundWorker 不想停止工作
BackgroundWorker does not want to stop working
我创建了一个简单的文件下载程序,下载第一组然后开始下载另一组并从列表中下载文件。
但有时会停在 3 个文件并下载。
我查了一下之前下载到100%还没有下载完成的问题。
Private Sub Download(ByVal Down As Integer)
On Error Resume Next
Dim req As System.Net.WebRequest
Dim resp As System.Net.WebResponse
TextBox1.Text = Convert.ToString("http://example.com/files/" + Convert.ToString(Down) + ".zip")
TextBox2.Text = Convert.ToString(Down)
req = Net.WebRequest.Create(TextBox1.Text)
resp = req.GetResponse
req.Method = Net.WebRequestMethods.Http.Get
download_size = resp.ContentLength
ProgressBar1.Maximum = download_size
Console.WriteLine(BackgroundWorker1.IsBusy)
BackgroundWorker1.RunWorkerAsync()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
On Error Resume Next
downloaded_size = My.Computer.FileSystem.GetFileInfo(FlatTextBox1.Text + "\" + TextBox2.Text + ".zip").Length
ProgressBar1.Value = downloaded_size
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
BackgroundWorker1.CancelAsync()
If Timer2.Enabled = False Then
Timer2.Start()'Timer 5sec to to start function Download(x) and ProgressBar set to 0
End If
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
On Error Resume Next
Console.WriteLine(TextBox1.Text + " | " + FlatTextBox1.Text + "\" + TextBox2.Text + ".zip")
My.Computer.Network.DownloadFile(TextBox1.Text, FlatTextBox1.Text + "\" + TextBox2.Text + ".zip", "", "", False, 360000, True)
End Sub
下载文件比使用 BackgroundWorker 更好的方法是 WebClient 和函数 DownloadFileAsync。
但需要在工具箱中启用WebClient。
Private Sub Download(ByVal Down As Integer)
TextBox2.Text = Convert.ToString(Down)
Dim Adresa As String = "http://example.com/files/" + Convert.ToString(Down) + ".zip"
WebClient1.DownloadFileAsync(New Uri(Adresa), FlatTextBox1.Text + "\" + TextBox2.Text + ".zip")
End Sub
Private Sub WebClient1_DownloadProgressChanged(sender As Object, e As Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub WebClient1_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles WebClient1.DownloadFileCompleted
ProgressBar1.Value = 0
'Here place script to download next file..
End Sub
我创建了一个简单的文件下载程序,下载第一组然后开始下载另一组并从列表中下载文件。 但有时会停在 3 个文件并下载。 我查了一下之前下载到100%还没有下载完成的问题。
Private Sub Download(ByVal Down As Integer)
On Error Resume Next
Dim req As System.Net.WebRequest
Dim resp As System.Net.WebResponse
TextBox1.Text = Convert.ToString("http://example.com/files/" + Convert.ToString(Down) + ".zip")
TextBox2.Text = Convert.ToString(Down)
req = Net.WebRequest.Create(TextBox1.Text)
resp = req.GetResponse
req.Method = Net.WebRequestMethods.Http.Get
download_size = resp.ContentLength
ProgressBar1.Maximum = download_size
Console.WriteLine(BackgroundWorker1.IsBusy)
BackgroundWorker1.RunWorkerAsync()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
On Error Resume Next
downloaded_size = My.Computer.FileSystem.GetFileInfo(FlatTextBox1.Text + "\" + TextBox2.Text + ".zip").Length
ProgressBar1.Value = downloaded_size
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
BackgroundWorker1.CancelAsync()
If Timer2.Enabled = False Then
Timer2.Start()'Timer 5sec to to start function Download(x) and ProgressBar set to 0
End If
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
On Error Resume Next
Console.WriteLine(TextBox1.Text + " | " + FlatTextBox1.Text + "\" + TextBox2.Text + ".zip")
My.Computer.Network.DownloadFile(TextBox1.Text, FlatTextBox1.Text + "\" + TextBox2.Text + ".zip", "", "", False, 360000, True)
End Sub
下载文件比使用 BackgroundWorker 更好的方法是 WebClient 和函数 DownloadFileAsync。 但需要在工具箱中启用WebClient。
Private Sub Download(ByVal Down As Integer)
TextBox2.Text = Convert.ToString(Down)
Dim Adresa As String = "http://example.com/files/" + Convert.ToString(Down) + ".zip"
WebClient1.DownloadFileAsync(New Uri(Adresa), FlatTextBox1.Text + "\" + TextBox2.Text + ".zip")
End Sub
Private Sub WebClient1_DownloadProgressChanged(sender As Object, e As Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub WebClient1_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles WebClient1.DownloadFileCompleted
ProgressBar1.Value = 0
'Here place script to download next file..
End Sub