Results 1 to 14 of 14

Thread: [RESOLVED] Downloading multiple files using threads

Threaded View

  1. #1

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

    Resolved [RESOLVED] Downloading multiple files using threads

    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:

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

    Hope this made sense.

    Can anyone provide any ideas of how this may be done?

    Many thanks in advance.
    Last edited by MadCatVB; Dec 15th, 2005 at 09:23 AM. Reason: Not Resolved after all

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