Results 1 to 14 of 14

Thread: [RESOLVED] Downloading multiple files using threads

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: Downloading multiple files using threads

    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 Sub
    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
    OK, a little update. After some perseverence i've found a way to get the downloads working and what seems like threaded properly.

    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.

    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
    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.

    How would i go about accessing the threads in the hashtable entries to check this.

    Thanks

  2. #2
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Re: [RESOLVED] Downloading multiple files using threads

    Hi,

    where you do define ht_ThreadData in your code? I had errors in this code
    Dim threadData As Hashtable = Hashtable.Synchronized(Me.ht_ThreadData)

    Error 1 'ht_ThreadData' is not a member of 'A.frmMain'.

    pls help, i'm new in vb.

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width