Hi,
I hope someone can help me with this. I'm currently writing a small app to download files from the web. At the moment i can download files individually but what i'd really like to do is to start a number of downloads simultaneously and queue the remaininfg ones to wait for download.
My thinking on this (and i could be wrong) was to use an individual thread for each download.
I'm not entirely sure how to go about setting this up. At present i have the code i want to run when the thread is initiated:
At present the user selects entries from a list of links and this then provides the files requiring download. When the download button is clicked i have this code so far:Code:Private Sub getFile() Dim wc_MyWebClient As New WebClient Dim sURL, sSavePath As String 'Access property storing the file URL SyncLock (Me) sURL = propURL End SyncLock 'Access property storing the save path. SyncLock (Me) sSavePath = propSavePath End SyncLock 'Download file here wc_MyWebClient.DownloadFile(sURL, sSavePath) wc_MyWebClient.Dispose() End Sub
The problem i have is here. I'm looking for a way that i could set this up so that only (say 3 for this example) threads are started and the rest have to wait until one thread finishes before another can start.Code:Private Sub btn_DownloadSelectedLinks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DownloadSelectedLinks.Click Dim sFolderPath As String If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then sFolderPath = FolderBrowserDialog1.SelectedPath 'MsgBox(sFolderPath) downloadSelectedFiles(sFolderPath) 'getFile 'loop through all selected indexes in the check list box For i As Integer = 0 To clb_Links.CheckedIndices.Count - 1 Step 1 propURL = clb_Links.CheckedItems.Item(i).ToString propSavePath = sFolderPath & "\" & Path.GetFileName(clb_Links.CheckedItems.Item(i).ToString) Dim t As New Thread(AddressOf getFile) t.Start() Next Else 'Dialogbox has been cancelled Exit Sub End If End Sub
Hope this made sense.
Can anyone provide any ideas of how this may be done?
Many thanks in advance.




Reply With Quote