我正在为我的Minecraft服务器上的mod创建一个安装程序/更新程序。我有它设置它,以便有两个文件,每行一个mod,代码比较两个并下载最新的mod,如果它们不匹配。目前我的下载设置如下:
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
使用此方法,表单在下载文件时完全冻结。
我想要的是每个下载时都有一个进度条移动,每次新的完成时重置为零。我知道使用this方法我可以下载一个文件,进度条也会顺利进行。但问题是,因为它使用异步下载,所以下载代码之下的任何代码都会在下载完成之前执行,这在尝试解压缩不存在的zip文件时会出现问题。
如果有人有解决方案,请提供代码示例,因为这实际上是我用任何语言编写的第一个程序,而不是教程。
答案 0 :(得分:0)
使用WebClient.DownloadFileAsync
并在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中处理完成的下载)。但是,在这种方法中,您根本不使用任何进度条,或者执行一些初学者不友好的编程来跟踪个人/总下载的进度。