
Originally Posted by
jmcilhinney
You can use a loop. There is only one secondary thread so only one file can be downloaded at a time. Do exactly what you just said and it will work.
How would I go about doing so with this sub?
Code:
Public Sub CallWorker()
Dim colFiles As New Collection
Dim intCtr As Integer
'the collection
colFiles.Add("GitPortable.zip")
colFiles.Add("ArduPilot-Arduino-1.0.3-windows.zip")
colFiles.Add("MHV_AVR_Tools_20121007.exe")
'the loop
For intCtr = 1 To colFiles.Count
If System.IO.File.Exists("C:\Windows\Temp\MPNGTemp\" & colFiles(intCtr)) Then
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
'starts the progress bar
Timer1.Start()
'starts the download sub
BackgroundWorker.RunWorkerAsync()
End If
'end of loop
Next intCtr
End Sub
That is the loop I am using to call the backgroundworker.
And it is calling this as the background worker
Code:
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 = "ArduPilot-Arduino-1.0.3-windows.zip" Then
link = "http://www.sshcs.com/MPNG/ArduPilot-Arduino-1.0.3-windows.zip"
ElseIf Filename = "GitPortable.zip" Then
link = "http://www.sshcs.com/MPNG/GitPortable.zip"
ElseIf Filename = "MHV_AVR_Tools_20121007.exe" Then
link = "http://firmware.diydrones.com/Tools/Arduino/MHV_AVR_Tools_20121007.exe"
ElseIf Filename = "" Then
MsgBox("Error No File Passed")
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
Each by themselves work fine and both work fine together if say 2 of the files are already there [it doesn't matter which 2]
How do I call a backgroundworker and halt any further execution of the sub until the initial loop finishes running and then running the 2nd loop and when it's done run the last loop?