[RESOLVED] WebClient.DownloadFileAsync when done?
Ola,
I'm downloading 5 files async. The downloaded files are replacing important files which are needed to load my app. and the executable itself. The problem is that I don't know when the last file is downloaded.
vb.net Code:
wclient1.DownloadFileAsync(New Uri(webFolder & fName1), Application.StartupPath & "\" & fName1)
wclient2.DownloadFileAsync(New Uri(webFolder & fName2), Application.StartupPath & "\" & fName2)
wclient3.DownloadFileAsync(New Uri(webFolder & fName3), Application.StartupPath & "\" & fName3)
wclient4.DownloadFileAsync(New Uri(webFolder & fName4), Application.StartupPath & "\" & fName4)
wclient5.DownloadFileAsync(New Uri(webFolder & fName5), Application.StartupPath & "\" & fName5)
Private Sub wClient1_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles wclient1.DownloadProgressChanged
Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percentage As Double = bytesIn / totalBytes * 100
download_progress.Value = Int32.Parse(Math.Truncate(percentage).ToString())
wclient1.Dispose()
End Sub
Private Sub wClient2_.....
'...etc
What I did was
vb.net Code:
Private Sub wclient5_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles wclient5.DownloadFileCompleted
System.Diagnostics.Process.Start(Application.StartupPath & "\my.exe")
wclient5.Dispose()
Application.Exit()
End Sub
...which is wrong, 'cause I don't know which file is downloaded last.
Anyone knows how to solve this? Thanks in advance.
Re: WebClient.DownloadFileAsync when done?
Just keep track via an Integer, adding 1 for each one done. In each completed file routine, just check to see if the Integer is 5 and you know everything is done.
Re: WebClient.DownloadFileAsync when done?
Good one. Let me give it a go and I'll let you know.
Re: WebClient.DownloadFileAsync when done?
Works perfect m8. Thanks. Was a pain to solve this issue today and when I figured what I did wrong I couldn't think of something simple. :rolleyes:
Thanks again. +REP
For the people who want to know how I solved it:
1) added a label on the form, which counts the "download-done";
2) added for each webclient I a DownloadFileCompleted;
3) added in each of the _DownloadFileCompleted the following code: lbl_progress.Text = CStr(CDbl(lbl_progress.Text) + 1)
And now I can continue ;)