下载多个文件异步VB.net [英] Downloading multiple files async VB.net

查看:28
本文介绍了下载多个文件异步VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 Minecraft 服务器上的模组创建安装程序/更新程序.我将它设置为有两个文件,每行一个 mod,代码比较这两个文件,如果它们不匹配,则下载最新的 mod.目前我有这样的下载设置:

I am creating an installer/updater for the mods on my Minecraft server. I have it set it up so that there are two files, one mod per line, and the code compares the two and downloads the latest mod if they don't match. Currently I have the downloads setup like this:

        For x = 2 To latestModCount - 1
            If currentModList(x) <> latestModList(x) Then
                IO.File.Delete(applicationLocation & "multimc\instances\Dan's Server\minecraft\mods\" & currentModList(x))
                My.Computer.Network.DownloadFile(OnlineFiles & "mods/" & latestModList(x), _
                        applicationLocation & "multimc\instances\Dan's Server\minecraft\mods\" & latestModList(x))
            End If

            'Updates currentModList to = latestModList
            objWriter.Write(latestModList(x))
        Next

使用此方法,在下载文件时表单会完全冻结.

With this method the form completely freezes while the files are downloading.

我想要的是在每次下载时都有一个进度条移动,每次新下载完成时重置为零.我知道使用 this 方法我可以下载一个文件,进度条会很好地移动.但问题在于,由于它使用异步下载,因此上述代码下方的任何代码都会在下载完成之前执行,这在尝试解压缩不存在的 zip 文件时会出现问题.

What I want is to have a progress bar move along as each one downloads, resetting to zero each time a new one is complete. I know that using this method I can get one file to download and the progress bar will move along nicely. The problem with this though, is that because it uses an asynchronous download, any code below the above code is executed before the downloads have finished, which becomes a problem when trying to unzip a zip file that doesn't exist.

如果有人有解决方案,请提供代码示例,因为这实际上是我第一个使用任何语言编写的程序,而不是教程.

If someone has a solution, please provide code examples as I this is actually my first program in any language, other than tutorial ones.

推荐答案

使用 WebClient.DownloadFileAsync 并在 client_DownloadCompleted 中处理完成的下载.

use WebClient.DownloadFileAsync and handle the completed download in client_DownloadCompleted.

伪代码:

Private i as Integer = -1

Private Sub StartNextDownload()
    Do
        i++
    Loop Until currentModList(i) <> latestModList(i)
    If i < latestModCount Then
        IO.File.Delete(applicationLocation & "multimc\instances\Dan's Server\minecraft\mods\" & currentModList(i))
        Dim client As WebClient = New WebClient
        AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
        AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
        client.DownloadFileAsync(New Uri(OnlineFiles & "mods/" & latestModList(i)), applicationLocation & "multimc\instances\Dan's Server\minecraft\mods\" & latestModList(i))
    End If
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
    progressBar.Value = Int32.Parse(Math.Truncate(percentage).ToString())
End Sub

Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    HandleCompletedDownload() 'unzip and other stuffs there
    StartNextDownload()
End Sub

另一种方法是一次触发所有下载,类似于您当前拥有的(同样,在 client_DownloadCompleted 中处理已完成的下载).但是,在这种方法中,您要么根本不使用进度条,要么执行一些对初学者不友好的编程来跟踪单个/总下载的进度.

Another approach is to fire all downloads at once, similar to what you currently have (again, handle the completed download in client_DownloadCompleted). However in this approach you either use no progress bar at all, or do some beginner-unfriendly programming to track progress of individual/total downloads.

这篇关于下载多个文件异步VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆