Code:Private Sub btn_DownalodViaHashtable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DownalodViaHashtable.Click Dim sFolderPath As String If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then sFolderPath = FolderBrowserDialog1.SelectedPath For i As Integer = 0 To clb_Links.CheckedIndices.Count - 1 Step 1 propLinkArray.Add(clb_Links.CheckedItems.Item(i).ToString) Next For i As Integer = 0 To propLinkArray.Count - 1 Step 1 propURL = propLinkArray.Item(i).ToString propSavePath = sFolderPath Dim t As New Threading.Thread(AddressOf getFileHashTable) Hashtable.Synchronized(Me.ht_ThreadData).Add(t, propURL) t.Start() Next Else 'Dialogbox has been cancelled Exit Sub End If End SubOK, a little update. After some perseverence i've found a way to get the downloads working and what seems like threaded properly.Code:Private Sub getFileHashTable() Dim threadData As Hashtable = Hashtable.Synchronized(Me.ht_ThreadData) 'Get the data for this thread. Dim data As String = DirectCast(threadData(Threading.Thread.CurrentThread), String) 'Code for downloading the file (called by each thread) Dim wc_MyWebClient As New WebClient wc_MyWebClient.DownloadFile(data, propSavePath & "\" & Path.GetFileName(data)) wc_MyWebClient.Dispose() 'Remove the data for this thread threadData.Remove(Threading.Thread.CurrentThread) End Sub
The array that i was adding to the hashtable as the value containing the file URL and the save path was not working as it was supposed to.
I'd still like to know why this is the case if anyone knows.
I now have to try and set it up so that only a specified number of downloads (threads) are run simultaneously. It worked bfore using the array and i have got some code that initially counts the entries in the hash table to find if is possible to add any more threads.
What i need to check for after any empty locations is if there are any threads that have finished running and do not flag up as IsAlive.Code:Private Sub checkHashTable() Dim threadData As Hashtable = Hashtable.Synchronized(Me.ht_ThreadData) 'Check the length of the Hashtable first of all to see if there is space for any new 'entries. If ht_ThreadData.Count < 3 Then 'MsgBox(ht_ThreadData.Count) 'There is an empty slot waiting to be filled. The element available 'is the count. propActiveArrayElement = ht_ThreadData.Count propThreadAvailable = True End If End Sub
How would i go about accessing the threads in the hashtable entries to check this.
Thanks




Reply With Quote