Hello, i have a program that downloads a file using the httpwebrequest/response, and it usually works, but sometimes it will freeze at a random part of the download without an error.
here is the code:
But im pretty sure my problem is in this part of the code:
here is the code:
Code:
'Creating the request and getting the response Dim theResponse As HttpWebResponse Dim theRequest As HttpWebRequest Try 'Checks if the file exist theRequest = WebRequest.Create(Me.Filelocation) theResponse = theRequest.GetResponse Catch ex As Exception MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _ "1) File doesn't exist" & ControlChars.CrLf & _ "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) Me.Invoke(cancelDelegate, True) Exit Sub End Try theRequest.Timeout = 30000 theRequest.KeepAlive = True Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes) Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts) Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create) 'Replacement for Stream.Position (webResponse stream doesn't support seek) Dim nRead As Integer 'To calculate the download speed Dim speedtimer As New Stopwatch Dim currentspeed As Double = -1 Dim readings As Integer = 0 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 Integer = (nRead / length) * 100 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 'Close the streams theResponse.GetResponseStream.Close() writeStream.Close() If Me.BackgroundWorker1.CancellationPending Then IO.File.Delete(Me.whereToSave) Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) Me.Invoke(cancelDelegate, True) Exit Sub End If Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) Try Me.Invoke(completeDelegate, False) Catch ex As Exception MsgBox(ex.Message) End Try
Code:
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 Integer = (nRead / length) * 100 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
Comment