HQuintas Posted December 29, 2016 at 08:02 AM Report Share #601558 Posted December 29, 2016 at 08:02 AM Boas, Quero fazer o download de vários ficheiros. Para resolver o problema coloquei um ciclo While client.IsBusy, para só avançar para o próximo quando estiver livre. Porque só pretendo que aplicação continue depois de ter feito todos os downloads. O que pretendo saber é se isto é o mais correcto ou se existe outra forma de o fazer. Obrigado. Imports System.Net Public Class frm_download Dim client As WebClient = New WebClient Private Sub Download_file() Dim fileReader As New WebClient() AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted client.UseDefaultCredentials = True client.Credentials = New NetworkCredential("user", "password") Dim files As New List(Of files_items) Dim file1 As New files_items Dim file2 As New files_items file1.origem = "http://helderquintas.16mb.com/pasta_protegida/www.txt" file1.destino = "xxx.txt" file2.origem = "http://helderquintas.16mb.com/pasta_protegida/www.txt" file2.destino = "www.txt" files.Add(file1) files.Add(file2) For Each a In files While client.IsBusy 'Enquanto estiver a fazer um download espera para fazer o próximo. End While client.DownloadFileAsync(New Uri(a.origem), a.destino) Next End Sub Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 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 ProgressBar1.Value = Int32.Parse(Math.Truncate(percentage).ToString()) End Sub Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) MessageBox.Show("Download Complete") End Sub Private Sub frm_update_Load(sender As Object, e As EventArgs) Handles MyBase.Load Download_file() End Sub End Class Public Class files_items Implements IEquatable(Of files_items) Public origem As String Public destino As String Public Function Equals1(other As files_items) As Boolean Implements IEquatable(Of files_items).Equals End Function End Class Link to comment Share on other sites More sharing options...
He B TeMy Posted January 4, 2017 at 07:35 PM Report Share #601787 Posted January 4, 2017 at 07:35 PM Podes fazer os downloads todos ao mesmo tempo, se forem ficheiros relativamente pequenos (< 1MB) é capaz de ser mais rápido, fazer Async e não permitir que ele faça mais do que um download ao mesmo tempo não faz sentido, mais valia fazer Download normal noutro thread e poupavas código. Link to comment Share on other sites More sharing options...
HQuintas Posted January 11, 2017 at 07:57 AM Author Report Share #601928 Posted January 11, 2017 at 07:57 AM Boas Estás a referir fazer com My.Computer.Network.DownloadFile ou client.DownloadFile(New Uri(a.origem), a.destino) ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now