locked
Code is downloading every file at the same time RRS feed

  • Question

  • Code is downloading every file at the same time.

    I am not sure what I am doing wrong. I just want to download a file then after it is done continue to next file. 

    You'll see in the code I have an array of files I need to download. These never change (Or very rarely change). I only want one file to download at a time. 

    Here is the code:

    Imports System.Net
    Imports System.ComponentModel
    
    
    Public Class Form1
    
        Dim WithEvents WClient As New WebClient
    
    
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim ftpsite As String = "ftp://myftp.com/"
    
            '*** For Third Party Files ***
            Dim DB_3rd_FTPFiles() As String = {"File1",
                                               "File2",
                                               "File3",
                                               "FIle4"}
    
    'These are different because they are in a different folder        
    Dim DB_BB_FTPFiles() As String = {"File5",
                                          "File6",
                                          "File7"}
    
            With My.Computer.FileSystem
                .CreateDirectory("C:\FakeEDrive\Install")
                .CreateDirectory("C:\FakeEDrive\Install\Folder1\")
                .CreateDirectory("C:\FakeEDrive\Install\Folder2\")
                .CreateDirectory("C:\FakeEDrive\Install\Folder3\")
                .CreateDirectory("C:\FakeEDrive\Install\Folder4\")
                
            End With
    
            For Each ThirdFTP As String In DB_3rd_FTPFiles
                If ThirdFTP = "File2.zip" Then
                    lbl_progress.Text = "Downloading file: " & ThirdFTP
                    WClient = New WebClient
    
    
                    Dim Downloadpath2 As New Uri(ftpsite & "SubFolder/" & ThirdFTP)
                    AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
                    AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
                    WClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)")
                    WClient.Credentials = New NetworkCredential("user", "password")
                    WClient.DownloadFileAsync(Downloadpath2, "C:\FakeEDrive\Install\Folder1\" & ThirdFTP)
    
                    WClient.Dispose()
                End If
    
                WClient = New WebClient
                WClient.Credentials = New NetworkCredential("user", "password")
                Dim Downloadpath As New Uri(ftpsite & ThirdFTP)
                AddHandler WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
                AddHandler WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
    
                Try
                    WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\Folder2\" & ThirdFTP)
    
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
    
                WClient.Dispose()
            Next
    
            For Each BBTSFTP As String In DB_BB_FTPFiles
                lbl_progress.Text = "Downloading file: " & BBTSFTP
                WClient = New WebClient
                WClient.Credentials = New NetworkCredential("user", "password")
                Dim Downloadpath As New Uri(ftpsite & "Folder/" & BBTSFTP)
                AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
                AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
    
                Try
                    WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\folder3\" & BBTSFTP)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
    
                WClient.Dispose()
            Next
    
        End Sub
    
        Public Sub WC_DownloadComplete(sender As Object, e As AsyncCompletedEventArgs)
    
            Using webClient As New Net.WebClient
                RemoveHandler webClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
            End Using
    
        End Sub
    
        Private Sub WClient_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    
            ProgressBar1.Value = e.ProgressPercentage
    
        End Sub
    
    End Class

    Monday, April 15, 2019 10:33 PM

Answers

  • When using DownloadFileAsync the process starts, your program code continues since this method is an asynchronous operation not a synchronous operation where a synchronous operation halts program execution until it's done so your code keeps going thus all of the operations are happening as you see it all at once.

    Welcome to the world of asynchronous programming. How to fix this, work with the DownloadFileCompleted delegate (which you have setup but need different logic) to know when to start the next file download.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Broggy69 Tuesday, April 16, 2019 4:14 PM
    Monday, April 15, 2019 11:15 PM
  • Hi

    There is also a non-async method:

    The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Tuesday, April 16, 2019 4:14 PM
    Monday, April 15, 2019 11:21 PM

All replies

  • When using DownloadFileAsync the process starts, your program code continues since this method is an asynchronous operation not a synchronous operation where a synchronous operation halts program execution until it's done so your code keeps going thus all of the operations are happening as you see it all at once.

    Welcome to the world of asynchronous programming. How to fix this, work with the DownloadFileCompleted delegate (which you have setup but need different logic) to know when to start the next file download.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Broggy69 Tuesday, April 16, 2019 4:14 PM
    Monday, April 15, 2019 11:15 PM
  • Hi

    There is also a non-async method:

    The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.


    Regards Les, Livingston, Scotland

    • Marked as answer by Broggy69 Tuesday, April 16, 2019 4:14 PM
    Monday, April 15, 2019 11:21 PM
  • Thank you both. That is working. Now to get my progress bar working
    Tuesday, April 16, 2019 4:14 PM