File Download

Discussion in 'Programming General' started by X Zero, Sep 29, 2007.

File Download
  1. Unread #1 - Sep 29, 2007 at 3:14 AM
  2. X Zero
    Joined:
    Jan 21, 2007
    Posts:
    577
    Referrals:
    0
    Sythe Gold:
    0

    X Zero Forum Addict

    File Download

    Ok, I am making a project that downloads multiple files.

    I have tried a do loop, but it misses files. I think it is because it just quickly runs trough.
    Code:
            Do Until count < 0
                count -= 1
                Dir = My.Application.Info.DirectoryPath & "/temp/" & file(count)
                Ir = URL.URL & file(count)
                WebClient.DownloadFileAsync(New Uri(Ir), Dir)
                Threading.Thread.Sleep(8000)
                System.Console.WriteLine("Downloaded: " & (file(count)))
            Loop
    What needs to download the list of files in the listbox one after the other. Help Please
     
  3. Unread #2 - Sep 29, 2007 at 4:19 AM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    File Download

    Ah, X Zero - EPIC FAIL.

    One statement, two words: FOR LOOP :D

    The Do-Until, Do-while etc. loops are generally used when you can't get a specific amount of times to loop the statements through. Do-loops will always execute at least once also, so using a do-loop in this type of code is probably buggy.

    for loop will loop from the minimum set number to the maximum set number, etc. So you can start at 0, and loop through to (List.ListCount - 1).

    I also notice that you are making the thread sleep rather than waiting until the file is downloaded ... unless it doesn't go on to Thread.Sleep() until the file is actually downloaded, so in that case forget this paragraph ;)

    Try
    Code:
    For i As Integer = 0 To count - 1 ' Assuming count is an absolute number which is bigger than or equal to 1
        Dir = My.Application.Info.DirectoryPath & "/temp/" & file(count)
        Ir = URL.URL & file(count)
        WebClient.DownloadFile(New Url(Ir), Dir)
        Console.WriteLine("Downloaded: " & (file(count)))
    Next
    not sure if it works, but worth a try.

    Async means that it doesn't block the calling Thread. So if you create a thread solely for downloading the files, then it won't block the main thread, and you don't have to estimate how long it will take to download by using Thread.Sleep().
     
  5. Unread #3 - Sep 29, 2007 at 5:10 AM
  6. X Zero
    Joined:
    Jan 21, 2007
    Posts:
    577
    Referrals:
    0
    Sythe Gold:
    0

    X Zero Forum Addict

    File Download

    I tried Loop
    ill post error

    Edit: Works thanks
     
< +help+ | Not able to post in VB section? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site