进度条活动可能非常不确定,在取消 WebClient.CancelAsync() 运行此客户端的函数时的最终行为中的预期操作和功能(如您认为它应该表现的那样)分配到应用程序内。我不知道下载完成后进度条会重置为最小值而没有被取消。所以,我将提供两个我用来玩的sn-ps代码,作为似乎是Windows窗体开发/ ASP.NET开发编程的“简单”起点的设计基础,'BAREBONES'版本,不使用处理下载活动更改的后台工作人员实例。
- 似乎“取消”正在进行的当前下载似乎引发了一些奇怪的行为,因为单击按钮似乎并没有正确引发取消事件以导致“停止”和“重置”中断取消按钮的按钮单击表单,与按钮单击事件中的 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
- 如果使用 BackgroundWokrer,那么它也会很有用,但是 BackgroundWorker 实例可以处理各种方式来执行此操作,例如使用可以处理表单下载活动重定向的类,但这需要更多代码显示活动/事件过程的粒度,从而使数字“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
结束子