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: - '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
But im pretty sure my problem is in this part of the 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
7 7745
Put a Try...Catch Statement around the WHOLE code, and then in the 'Catch Ex as Exception' line, under that have a message box showing the whole error.
Example: - Try
-
'All Code
-
Catch ex As Exception
-
MessageBox.Show(ex.ToString())
-
End Try
This should give you an error the error that's happening some point in the code, post back the error and line number so we can help you furthermore. :)
joedeene
i tried that already, but it doesnt show that anythings wrong, the whole program still works but the downloaded freezes at a random place... and it doesnt do it all the time it, its just random.
Hmm...This has occurred to me a while ago when I was working with downloading files, but since you're in VB.NET, why not use the My.Computer.Network.DownloadFile() Method, it's kind of the lazy way, but if it works?
joedeene
because i want to get the speed and progress of the download
Hmm these lines:
theRequest.Timeout = 30000
theRequest.KeepAlive = True
have no effect since you have already called for the response.
Your code does lot of work, is it possible it is getting stuck in a loop somewhere.
Have you set a breakpoint and stepped through your code?
its usually just a random point that it just decides to stop downloading, i did find a quick fix though... instead of using a backgroundworker i just put it all in a different sub and called application.doevents, and this seemed to fix it.
That was as I thought.
You are in a busy loop somewhere trying to get the rest of the data, but the managed object doesn't know more data has come in, because it never had the chance to get its messages (the doevents() call)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
I haven't been able to reach www.php.net for days. Most of the rest of
the web is working for me, though I've bad trouble reaching any
English sites. Anyone else having trouble?
|
by: Brian Loesgen |
last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25,
2003 at the Scripps Ranch Library.
Scripps Ranch Library
10301 Scripps Lake Drive
San Diego, CA 92131-1026
Please join us...
|
by: Eric Clapton |
last post by:
When should I use vb.net and when I should use c#.net? What is pros and
cons?
|
by: Terry |
last post by:
Hi, I need some feedback.
We are converting to .Net and we are trying to decide on whether to use
VB.net or C#.net.
As far as our current systems, they will probably be rewritten in ASP.Net.
I...
|
by: pieter.breed |
last post by:
Hi All,
The company I work for has traditionally used COM/ActiveX for the
solutions that it provides. We are in the process of moving to .NET and
a few applications have been written in VB.NET...
|
by: CMan |
last post by:
Hi,
We are currently trying to install .Net Framework v.1.1 on a server which
already has v1.0.
We are receiving the following error.
Error 1704.An installation for Microsoft .NET Framework...
|
by: lester |
last post by:
a pre-beginner's question:
what is the pros and cons of .net, compared to ++
I am wondering what can I get if I continue to learn C# after I have learned
C --> C++ --> C# ??
I think there...
|
by: Herr Lucifer |
last post by:
As the founder of .NET framework, Microsoft claims that it invention will be
the next best platform for programming in a near future. Now it is 2005,
..NET is 5 years old, and can talk and walk for...
|
by: Scott Lee via .NET 247 |
last post by:
(Please pardon me if this is a duplicate --- not sure if messageposted)
Hello,
I have been working my tail off developing an ASP.NETapplication. Now that the project is nearing completion,...
|
by: gulu man |
last post by:
Hi,
What is the substitute for COM objects in .NET?
How can I create something similar to com in .net?
Is it still possible?
Thank you
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |