• Log in or sign up for an account here: https://www.sythe.org/login

File Download

X Zero

Forum Addict
Joined
Jan 21, 2007
Sythe Gold
0
Messages
577
Location
Australia, The South
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
 
Member of the Month Winner
Sythe's 10th Anniversary
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().
 
Runescape Gamblingrunescape gambling
Back
Top