Odd question and hopefully easy answer
I am trying to loop through a collection and download a set of files but because
each download is using the same backgroundworker I am running into a problem with collisions and
other stuff


When I click the button I get this error

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.


If only one file is missing it seems to download it ok. But if more than one file is missing it errors.

I need help figuring out how to delay the next download until the one before it is done and still update the progress bar.

I'm guessing [with my SO limited knowledge of VB] that because I am running a backgroundworker and it's set to async that I
somekind of way to run the worker then end it before starting the next. But how is totally eluding me.


I have a form called DLForm
5 Labels
1 RichTextBox
1 ProgressBar
1 Button
1 BackgroundWorker
1 Timer

Code:
Imports System.Net
Imports System.IO
'Imports Ionic.Zip
Imports System.Runtime.InteropServices
Public Class DLForm
#Region " Functions and Constants "
    <DllImport("user32.dll")> _
    Public Shared Function ReleaseCapture() As Boolean
    End Function
    <DllImport("user32.dll")> _
    Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function
    Public Const WM_NCLBUTTONDOWN As Integer = &HA1
    Public Const HTBORDER As Integer = 18
    Public Const HTBOTTOM As Integer = 15
    Public Const HTBOTTOMLEFT As Integer = 16
    Public Const HTBOTTOMRIGHT As Integer = 17
    Public Const HTCAPTION As Integer = 2
    Public Const HTLEFT As Integer = 10
    Public Const HTRIGHT As Integer = 11
    Public Const HTTOP As Integer = 12
    Public Const HTTOPLEFT As Integer = 13
    Public Const HTTOPRIGHT As Integer = 14
#End Region
#Region " Moving & Resizing methods "
    Public Sub MoveForm()
        ReleaseCapture()
        SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End Sub
#End Region
    Private Sub SavePosition(ByVal frm As Form, ByVal app_name As String)
        SaveSetting(app_name, "Geometry", "WindowState", frm.WindowState)
        If frm.WindowState = FormWindowState.Normal Then
            SaveSetting(app_name, "Geometry", "Left", frm.Left)
            SaveSetting(app_name, "Geometry", "Top", frm.Top)
            SaveSetting(app_name, "Geometry", "Width", frm.Width)
            SaveSetting(app_name, "Geometry", "Height", frm.Height)
        Else
            SaveSetting(app_name, "Geometry", "Left", frm.RestoreBounds.Left)
            SaveSetting(app_name, "Geometry", "Top", frm.RestoreBounds.Top)
            SaveSetting(app_name, "Geometry", "Width", frm.RestoreBounds.Width)
            SaveSetting(app_name, "Geometry", "Height", frm.RestoreBounds.Height)
        End If
    End Sub
    Private Sub RestorePosition(ByVal frm As Form, ByVal app_name As String)
        frm.SetBounds( _
            GetSetting(app_name, "Geometry", "Left", Me.RestoreBounds.Left), _
            GetSetting(app_name, "Geometry", "Top", Me.RestoreBounds.Top), _
            GetSetting(app_name, "Geometry", "Width", Me.RestoreBounds.Width), _
            GetSetting(app_name, "Geometry", "Height", Me.RestoreBounds.Height) _
        )
        Me.WindowState = GetSetting(app_name, "Geometry", "WindowState", Me.WindowState)
    End Sub
    Public link As String
    Public Filename As String
    Public FilenameGP As String
    Public FilenameIDE As String
    Public FilenameMHV As String
    Private Sub DLForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RestorePosition(Me, "DL4")
        Label5.Visible = False
    End Sub
    Private Sub DLForm_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        SavePosition(Me, "DL4")
    End Sub
    Private Sub Downloader_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
        'mod this
        Filename = Label5.Text
        File.Delete("C:\Windows\Temp\MPNGTemp\" & Filename)
        If Filename = "GitPortable.zip" Then
            link = "http://www.sshcs.com/MPNG/ArduPilot-Arduino-1.0.3-windows.zip"
        End If
        If Filename = "ArduPilot-Arduino-1.0.3-windows.zip" Then
            link = "http://www.sshcs.com/MPNG/GitPortable.zip"
        End If
        If Filename = "MHV_AVR_Tools_20121007.exe" Then
            link = "http://firmware.diydrones.com/Tools/Arduino/MHV_AVR_Tools_20121007.exe"
        End If
        Try
            Dim size As Integer
            Dim wr As WebRequest
            wr = WebRequest.Create(link)
            Dim webr As WebResponse = wr.GetResponse
            size = webr.ContentLength
            size = size / 1024
            ProgressBar1.Maximum = size
            Label2.Text = size
            Dim wc As New WebClient
            wc.DownloadFile(link, "C:\Windows\Temp\MPNGTemp\" & Filename)
            RichTextBox1.AppendText("Finished downloading " & Filename & vbCrLf)
        Catch ex As Exception
        End Try
        Exit Sub
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Filename = Label5.Text
        Dim amount As Integer
        Try
            If System.IO.File.Exists("C:\Windows\Temp\MPNGTemp\" & Filename) Then
                Dim o As New System.IO.FileInfo("C:\Windows\Temp\MPNGTemp\" & Filename)
                amount = o.Length
                amount = amount / 1024
                Label3.Text = amount
                ProgressBar1.Value = amount
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Downloader_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
        RichTextBox1.AppendText("Finished Downloading" & vbCrLf)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim colFiles As New Collection
        Dim intCtr As Integer
        colFiles.Add("GitPortable.zip")
        colFiles.Add("ArduPilot-Arduino-1.0.3-windows.zip")
        colFiles.Add("MHV_AVR_Tools_20121007.exe")

        For intCtr = 1 To colFiles.Count
				Label5.Text = colFiles(intCtr)
            If System.IO.File.Exists("C:\Windows\Temp\MPNGTemp\" & colFiles(intCtr)) Then
                'already there so skip download
                RichTextBox1.AppendText(colFiles(intCtr) & " Already exists so skipping" & vbCrLf)
            Else
                Label5.Text = colFiles(intCtr)
                RichTextBox1.AppendText(colFiles(intCtr) & " does not exist so downloading" & vbCrLf)
                Control.CheckForIllegalCrossThreadCalls = False
                Timer1.Start()
                BackgroundWorker.RunWorkerAsync()
            End If
        Next intCtr
    End Sub
End Class