0

作为一些练习,我试图让我的程序通过进度条下载文件。它下载它很好并且进度条跟随但问题是下载完成后它不会重置。这是按钮和栏的代码。

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    WC.DownloadFileAsync(New Uri("imageurlhere"), "c:\myfile.jpg")
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        ProgressBar1.Value = ProgressBar1.Minimum
    End If
End Sub

Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
End Sub
4

3 回答 3

0

试试这个

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    WC.DownloadFileAsync(New Uri("imageurlhere"), "c:\myfile.jpg")
End Sub

Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        ProgressBar1.Value = ProgressBar1.Minimum
    End If
End Sub
于 2013-05-05T15:28:07.493 回答
0

在取消 WebClient.CancelAsync() 运行此客户端分配给其中的函数的最终行为中,进度条活动可能非常不确定,就按预期操作和运行(如您认为它应该表现的那样)而言应用程序。我不知道下载完成后进度条会重置为最小值而没有被取消。因此,我将提供两个我用来玩的代码片段,作为似乎是 Windows 窗体开发 / ASP.NET 开发编程的“简单”起点的设计基础,“BAREBONES”版本,不使用背景处理下载活动更改的工人实例。

  1. 似乎“取消”正在进行的当前下载似乎引发了一些古怪的行为,因为单击按钮似乎并没有正确引发取消事件,从而导致表单上的“停止”和“重置”中断取消按钮的按钮单击,与按钮单击事件中的 CancelAsync() 一起使用。因此,我看到了几种不同的行为,具体取决于分配给此事件的代码逻辑中实现的进度条属性/方法的内容。

因此,似乎我只有一个简单的解决方法,即使用 MessageBox.Show() 中断或自定义表单,用作消息框表单的相同目的。

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click

    ' THE Kill() IS NOT NECESSARY IF THE MessageBox Show() is IMPLEMENTED AND USED
    'Kill(TextBox2.Text) ' since the Cancel button does not fully stop the download
    ' and clear the prgress bar to reset that to "0", this is used to stop the download
    ' at the root path of the saved file destinatio !!!!n
    download.CancelAsync()
    ProgressBar1.Disposing.ToString()
    ' ProgressBar1.Dispose() ' hides the progress bar here
    ProgressBar1.Update()
    ProgressBar1.Value = 100
    ProgressBar1.Show()
    LastCount = 0

    ' Label4.ResetText()
    Label4.BackColor = Color.LightYellow
    Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED .. A - N - D .. ALSO ABORTED !!!!!!!"
    Label4.Visible = True
    ' Label5.ResetText()
    Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
    Label5.Visible = True
    'Label6.ResetText()
    Label6.Text = "Saved Copy of File Download is : "
    'Label7.ResetText()
    Label7.Text = "Download Source File Size is : "
    Label8.ResetText()
    Label8.Text = ""
    'Label10.ResetText()
    'Label10.Text += Label10.Text & " was the previous file downloading transfer rate...."


    ' trows error if there is not a file in the directory
    ' IO.File.Delete(savedLocation) ' delete the file in the chosen directory, if present

    cmdsave.Enabled = True
    Button1.Enabled = False
    Button2.Enabled = False
    Button3.Enabled = False
    'Label4.Visible = True ' to alleviate the text that will be shown as the "old"
    'Label5.Visible = True ' values of the cancelled download, so make these not visible
    ' ...... make the labels "invislbe" now, but make them "visible" when the
    ' ...... cmdsae button is clicked, or see if the form load can make them
    ' ...... visbile again  -->>> POST THE CANCELLATION MESSAGE IN
    ' .....  TEXTBOX1 OR LABEL



    If ProgressBar1.Value = 100 Then

        Label4.ResetText()
        Label4.BackColor = Color.Transparent
        Label4.Text = ""
        ' Button1.Enabled = False ' DOES NOT WORK HERE !!
        MessageBox.Show("FILE DOWNLOAD WAS CANCELLED / ABORTED !!")

        Button1.Enabled = False ' DOES W-O-R-K -- GREAT -- HERE !!
        ProgressBar1.Value = 0
        ProgressBar1.Update()
        ' ProgressBar1.Hide()
        ' ProgressBar1.Visible = False
        'Label4.ResetText()
        Label4.BackColor = Color.Yellow
        Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED ABD ABORTED!!!!!"
        Label4.Visible = True
        ' Label5.ResetText()
        Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
        Label5.Visible = True
        'Label6.ResetText()
        Label6.Text = "Saved Copy of File Download is : "
        'Label7.ResetText()
        Label7.BackColor = Color.Transparent
        Label7.Text = "Download Source File Size is : "
        Label8.ResetText()
        Label8.Text = ""
        'Label10.ResetText()
        Label10.Text = Label10.Text & " was the previous file downloading transfer rate...."

        Return
    End If

    download.Dispose()

    MessageBox.Show("")

    Return

    Kill(TextBox1.Text)

End Sub
  1. 如果使用了 BackgroundWokrer,那么它也会很有用,但是 BackgroundWorker 实例可以处理各种方式来执行此操作,例如使用可以处理表单下载活动重定向的 Class,但这需要更多代码来显示粒度活动/事件处理,从而使数字“1”。选项作为手头的最佳解决方案,可能不是完全用于代码模块化/模块化,而是用于代码活动区分而不编写更多代码并将其包装为测试中可能的故障点,这是我的选择/选项实现!
于 2021-07-12T23:31:58.540 回答
0

在取消 WebClient.CancelAsync() 运行此客户端分配给其中的函数的最终行为中,进度条活动可能非常不确定,就按预期操作和运行(如您认为它应该表现的那样)而言应用程序。我不知道下载完成后进度条会重置为最小值而没有被取消。因此,我将提供两个我用来玩的代码片段,作为似乎是 Windows 窗体开发 / ASP.NET 开发编程的“简单”起点的设计基础,“BAREBONES”版本,不使用背景处理下载活动更改的工人实例。

  1. 似乎“取消”正在进行的当前下载似乎引发了一些古怪的行为,因为单击按钮似乎并没有正确引发取消事件,从而导致表单上的“停止”和“重置”中断取消按钮的按钮单击,与按钮单击事件中的 CancelAsync() 一起使用。因此,我看到了几种不同的行为,具体取决于分配给此事件的代码逻辑中实现的进度条属性/方法的内容。

因此,似乎我只有一个简单的解决方法,即使用 MessageBox.Show() 中断或自定义表单,用作消息框表单的相同目的。

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click

    ' THE Kill() IS NOT NECESSARY IF THE MessageBox Show() is IMPLEMENTED AND USED
    'Kill(TextBox2.Text) ' since the Cancel button does not fully stop the download
    ' and clear the prgress bar to reset that to "0", this is used to stop the download
    ' at the root path of the saved file destinatio !!!!n
    download.CancelAsync()
    ProgressBar1.Disposing.ToString()
    ' ProgressBar1.Dispose() ' hides the progress bar here
    ProgressBar1.Update()
    ProgressBar1.Value = 100
    ProgressBar1.Show()
    LastCount = 0

    ' Label4.ResetText()
    Label4.BackColor = Color.LightYellow
    Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED .. A - N - D .. ALSO ABORTED !!!!!!!"
    Label4.Visible = True
    ' Label5.ResetText()
    Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
    Label5.Visible = True
    'Label6.ResetText()
    Label6.Text = "Saved Copy of File Download is : "
    'Label7.ResetText()
    Label7.Text = "Download Source File Size is : "
    Label8.ResetText()
    Label8.Text = ""
    'Label10.ResetText()
    'Label10.Text += Label10.Text & " was the previous file downloading transfer rate...."


    ' trows error if there is not a file in the directory
    ' IO.File.Delete(savedLocation) ' delete the file in the chosen directory, if present

    cmdsave.Enabled = True
    Button1.Enabled = False
    Button2.Enabled = False
    Button3.Enabled = False
    'Label4.Visible = True ' to alleviate the text that will be shown as the "old"
    'Label5.Visible = True ' values of the cancelled download, so make these not visible
    ' ...... make the labels "invislbe" now, but make them "visible" when the
    ' ...... cmdsae button is clicked, or see if the form load can make them
    ' ...... visbile again  -->>> POST THE CANCELLATION MESSAGE IN
    ' .....  TEXTBOX1 OR LABEL



    If ProgressBar1.Value = 100 Then

        Label4.ResetText()
        Label4.BackColor = Color.Transparent
        Label4.Text = ""
        ' Button1.Enabled = False ' DOES NOT WORK HERE !!
        MessageBox.Show("FILE DOWNLOAD WAS CANCELLED / ABORTED !!")

        Button1.Enabled = False ' DOES W-O-R-K -- GREAT -- HERE !!
        ProgressBar1.Value = 0
        ProgressBar1.Update()
        ' ProgressBar1.Hide()
        ' ProgressBar1.Visible = False
        'Label4.ResetText()
        Label4.BackColor = Color.Yellow
        Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED ABD ABORTED!!!!!"
        Label4.Visible = True
        ' Label5.ResetText()
        Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
        Label5.Visible = True
        'Label6.ResetText()
        Label6.Text = "Saved Copy of File Download is : "
        'Label7.ResetText()
        Label7.BackColor = Color.Transparent
        Label7.Text = "Download Source File Size is : "
        Label8.ResetText()
        Label8.Text = ""
        'Label10.ResetText()
        Label10.Text = Label10.Text & " was the previous file downloading transfer rate...."

        Return
    End If

    download.Dispose()

    MessageBox.Show("")

    Return

    Kill(TextBox1.Text)

End Sub
  1. 如果使用了 BackgroundWokrer,那么它也会很有用,但是 BackgroundWorker 实例可以处理各种方式来执行此操作,例如使用可以处理表单下载活动重定向的 Class,但这需要更多代码来显示粒度活动/事件处理,从而使数字“1”。选项作为手头的最佳解决方案,可能不是完全用于代码模块化/模块化,而是用于代码活动区分而不编写更多代码并将其包装为测试中可能的故障点,这是我的选择/选项实现!

值得一提的是,相对于下载后更改和“归零”进度条的典型进度更改过程具有如上所述的正常语法/语义表达,即 matzone 在 THIS POST 中给出的答案,并且可能另一位专家或知识渊博的 .NET 程序员对此问题给出的答案。我个人处理基本进度条“重置”,在成功并完成下载过程后下载文件,并在取消当前、活动下载(进行中)时,使用 WEBCLIENT CONTROL 的 DownloadFileAsync 作为:

私人子......

    ProgressBar1.Value = e.ProgressPercentage


If e.ProgressPercentage > LastCount Then

    If ProgressBar1.Value = ProgressBar1.Maximum Then

        Label4.ResetText()
        Label4.BackColor = Color.AntiqueWhite
        Label4.Text = "File Download Is Complete"
        Label7.ResetText()
        Label7.BackColor = Color.Gold
        Label7.Text = "DOWNLOAD STATUS : File was successfully downlaoded !!"

        Label8.ResetText()
        Label8.Text = "LAST DOWNLOADED FILE : " + TextBox2.Text.ToString +
    " with a size of " & GetDownloadSize(TextBox1.Text) & " KB , " +
    "was downloaded on" + DateTime.Now + " from the URL / URI at " +
    TextBox1.Text.ToString + " with a download transfer rate of " +
    "approximately, ( ~ ) " + (e.BytesReceived / (DirectCast(e.UserState, Stopwatch).ElapsedMilliseconds / 1000.0#)).ToString("#") & " KB. "

' 如果需要,执行其他表单功能/特性,例如重置表单的按钮,如下所示:

        cmdsave.Enabled = True
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = True
        viewDLfile.Enabled = True
        cmdPaste.Enabled = False
        ProgressBar1.Value = Nothing
        ' TextBox1.Clear() ; seems to throw a 

        Button1.Focus()

        LastCount = 0

        Return
    End If

End If

结束子

于 2021-07-13T13:58:37.310 回答