等待下载,直到下载上一个文件

时间:2015-09-16 12:49:29

标签: vb.net loops asynchronous download webclient

我有一个DataTable,其中包含文件信息。我遍历这个表,一个状态控制这些文件发生了什么(删除,添加新的,更新)。在添加new和update的情况下,必须从服务器下载单个文件并保存在文件系统中。这很好,没有任何问题。

现在有一个新请求。目前,用户最后只得到文件下载完成的注释。现在,用户希望在下载过程中获得此提示下载了多少。我怎么能实现这个?上次下载完成后,必须启动新的文件下载。

我目前的代码如下:

Dim oDownloadFileClient As New System.Net.WebClient

For Each Row As DataRow In fileTable.Rows
    Console.WriteLine(String.Format("Start processing file {0}", Row("FileName").Value))

    Select Case Row("State").Value
        Case 0
            'delete file
        Case 1 'new file
            Try
                'download file
                Dim oFile As Byte() = oDownloadFileClient.DownloadData("<DownloadPath>")
                System.IO.File.WriteAllBytes(FileName, oFile)

                Console.WriteLine(String.Format("File {0} saved.", Row("FileName").Value))
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Case 2, 3 'update file
            Try
                Dim oFile As Byte() = oDownloadFileClient.DownloadData("<DownloadPath>")
                System.IO.File.WriteAllBytes(FileName, oFile)

                Console.WriteLine(String.Format("File {0} updated.", Row("FileName").Value))
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Case Else
    End Select
Next

提前感谢您的帮助。

已编辑的代码

Dim oDownloadFileClient As New System.Net.WebClient
AddHandler oDownloadFileClient.DownloadProgressChanged, AddressOf DownloadProgressCallback

For Each Row As DataRow In fileTable.Rows
    Console.WriteLine(String.Format("Start processing file {0}", Row("FileName").Value))

    Select Case Row("State").Value
        Case 0
            'delete file
        Case 1 'new file
            Try
                'download file
                oDownloadFileClient.DownloadFileAsync("<DownloadPath>", FileName)

                While (oDownloadFileClient.IsBusy)

                End While

                Console.WriteLine(String.Format("File {0} saved.", Row("FileName").Value))
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Case 2, 3 'update file
            Try
                oDownloadFileClient.DownloadFileAsync("<DownloadPath>", FileName)

                While (oDownloadFileClient.IsBusy)

                End While

                Console.WriteLine(String.Format("File {0} updated.", Row("FileName").Value))
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Case Else
    End Select
Next
Private Sub DownloadProgressCallback(sender As Object, e As Net.DownloadProgressChangedEventArgs)
    Console.WriteLine(String.Format("{0}% downloaded"), e.ProgressPercentage)
End Sub

0 个答案:

没有答案