【发布时间】:2014-09-06 13:38:31
【问题描述】:
我想使用 backgroundworker 下载两个 zip 文件,但是当我想下载文件时,它给我的错误是 backgoundworker 很忙,无法同时运行多个任务。这是代码
Imports System.Net
Public Class mainForm
Dim whereToSave As String 'Where the program save the file
Private files() As String = {}
Private dlCount As Integer = 0
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)
Public Sub DownloadComplete(ByVal cancelled As Boolean)
'Me.txtFileName.Enabled = True
Me.btnDownload.Enabled = True
Me.btnCancel.Enabled = False
If cancelled Then
Me.Label4.Text = "Cancelled"
MessageBox.Show("Download aborted", "Aborted", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Me.Label4.Text = "Successfully downloaded"
MessageBox.Show("Successfully downloaded!", "All OK", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Me.ProgressBar1.Value = 0
'Me.Label5.Text = "Downloading: "
'Me.Label6.Text = "Save to: "
'Me.Label3.Text = "File size: "
'Me.Label2.Text = "Download speed: "
Me.Label4.Text = ""
End Sub
点击下载按钮
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
Me.btnDownload.Enabled = False
Me.btnCancel.Enabled = True
Dim TestString As String = "http://ec2-54-76-30-19.eu-west-1.compute.amazonaws.com/manapps/IDGo800_Minidriver_32.zip," & _
"http://ec2-54-76-30-19.eu-west-1.compute.amazonaws.com/manapps/IDGo800_Minidriver_64.zip"
Dim strFile As String
Dim length As Long
Dim position As Integer
Dim percent As Integer
Dim speed As Double
files = TestString.Split(CChar(","))
Dim a As String
For Each a In files
Dim b() As String = a.Split("/"c)
strFile = b(b.Length - 1)
'Dim strFileName As String = ""
'MsgBox(a)
'MsgBox(strFile)
Me.whereToSave = "C:\Temp\" & strFile
Me.DownloadingLabel.Text = "Downloading: " & strFile
Me.SaveToLabel.Text = "Save To: " & strFile
Me.FileSizeLabel.Text = "File Size: " & Math.Round((length / 1024), 2) & " KB"
Me.Label4.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
If speed = -1 Then
Me.DownloadSpeedLabel.Text = "Speed: calculating..."
Else
Me.DownloadSpeedLabel.Text = "Speed: " & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.ProgressBar1.Value = percent
dlCount = 0
StartDownload()
Next
End Sub
调用下载函数
Private Sub StartDownload()
dlCount += 1
If dlCount <= files.Length Then
BackgroundWorker1.RunWorkerAsync(files(dlCount - 1))
Else
MessageBox.Show("All files Downloaded")
'Button1.Enabled = True
End If
End Sub
后台工作人员
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim arg As String = DirectCast(e.Argument, String)
'Creating the request and getting the response
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try 'Checks if the file exist
theRequest = WebRequest.Create(arg)
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)
Exit Sub
End Try
End Sub
如果完成,它将调用 backgroundcomplete
Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
StartDownload()
End Sub
【问题讨论】:
标签: vb.net backgroundworker downloadfile