我有一个可以下载文件的代码,但我偶然发现了这个错误 “算术操作导致溢出” 。这是代码:
Dim nRead As Integer
'To calculate the download speed
Dim speedtimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
theRequest.AddRange(nRead)
Do
If BackgroundWorker1.CancellationPending Then 'If user abort download
Exit Do
End If
speedtimer.Start()
Dim readBytes(4095) As Byte
Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Short = (nRead * 100) / length
Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
speedtimer.Stop()
readings += 1
If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
speedtimer.Reset()
readings = 0
End If
Loop
它停留在
Dim percent As Short = (nRead * 100) / length
答案 0 :(得分:0)
单击以使用所有信息下载.NET中的文件:进度条,下载速度,支持取消和恢复
http://www.codeproject.com/Articles/17979/Downloading-Files-in-NET-With-All-Information-Prog
答案 1 :(得分:0)
我已经找到了答案
Dim percent As Short = nRead / Length * 100