等到下载完成后再开始其他任务

时间:2017-08-01 17:39:18

标签: vb.net user-interface

我正在尝试下载文件,然后运行一些adb命令。但是在下载时,我无法使用

更新进度条
downloadFile(url,filename)` command.

有点搜索它说该命令阻止了UI线程,所以我决定使用Task.Run()(我上一篇文章的解决方案在阻止UIThread时运行ADB命令)。

这没有任何区别。我找到的另一个解决方案是使用

downloadFileAsync(url, filename)

进度条正在更新!

但是,在下载文件之前ADB命令正在运行!它们在之后被声明,但是在下载文件之前它们仍在运行,这是我不想要的。

以下是代码:

Private Sub btnFlashRecovery_Click(sender As Object, e As EventArgs) Handles btnFlashRecovery.Click
                    'Insert ommited Code here (removed to simplify question) 
                    'id is variable obtained from a previous code that was ommited here        
                    Dim fileName As String = "downloads/twrp-" & id & ".img"
                    DownloadFile(url, fileName)

                    'run the right commands

                    LabelToOutput = txtBoxRecovery
                    Dim commands(3, 3) As String
                    commands = {{"adb", "reboot bootloader", "Rebooting to bootloader"},
                                            {"fastboot", "flash recovery" & "downloads/twrp-3.1.1-0.img", "Flashing recovery: (make sure device is plugged, otherwise it will not output anything)"},
                                            {"fastboot", "reboot", "Rebooting device"}

                                        }
                    'Task to run after
                    Task.Run(Sub() runComands(commands))


End Sub

Private Sub UpdateProgressBar(ByVal a As Integer)
    If Me.InvokeRequired Then
        Dim args() As String = {a}
        Me.Invoke(New Action(Of String)(AddressOf UpdateProgressBar), args)
        Return
    End If
    ProgressBar1.Value = CInt(a)

End Sub

Public Sub DownloadFile(urlAddress As String, location As String)
    Using webClient = New WebClient()
        AddHandler webClient.DownloadFileCompleted, AddressOf Completed
        AddHandler webClient.DownloadProgressChanged, AddressOf ProgressChanged



        Try
            ' Start downloading the file

            webClient.DownloadFileAsync(New Uri(urlAddress), location)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Using

End Sub

' The event that will fire whenever the progress of the WebClient is changed
Private Sub ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)

    'Console.WriteLine(e.ProgressPercentage)
    ' Update the progressbar percentage only when the value is not the same.
    UpdateProgressBar(e.ProgressPercentage)



End Sub

' The event that will trigger when the WebClient is completed
Private Sub Completed(sender As Object, e As AsyncCompletedEventArgs)


    If e.Cancelled = True Then
        MessageBox.Show("Download has been canceled.")
    Else
        MessageBox.Show("Download completed!")
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

这样做:

'Need Async keyword with the method
Private Async Sub btnFlashRecovery_Click(sender As Object, e As EventArgs) Handles btnFlashRecovery.Click
    'Insert ommited Code here (removed to simplify question) 

    'id is variable obtained from a previous code that was ommited here        
     Dim fileName As String = "downloads/twrp-" & id & ".img"
    'You need to AWAIT the result of the task
     Await Task.Run(Sub() DownloadFile(url, fileName))

    'run the right commands
    LabelToOutput = txtBoxRecovery
    Dim commands(3, 3) As String
    commands = { {"adb", "reboot bootloader", "Rebooting to bootloader"},
                 {"fastboot", "flash recovery" & "downloads/twrp-3.1.1-0.img", "Flashing recovery: (make sure device is plugged, otherwise it will not output anything)"},
                 {"fastboot", "reboot", "Rebooting device"}
               }
    'Task to run after
    'Await here, too, to allow the UI to remain responsive
    Await Task.Run(Sub() runComands(commands))
End Sub