DownloadFile locks application

M

Morten Snedker

I have this Class:

Imports System.Net
Public Class DownloadFile
Private _webClt As WebClient
Private _url As String
Private _file As String

Sub New(ByVal urlSource As String, ByVal fileDestination As
String)
_url = urlSource
_file = fileDestination
End Sub
Sub DownloadFile()

_webClt = New WebClient
_webClt.DownloadFile(_url, _file)
_webClt = Nothing

End Sub
End Class


It works well. However, I wish to continuesly read the size of the
file written (show activity for the user), but DownloadFile locks the
application until it has finished downloading the file.

How can I make the form-update independent from the download - or
otherwise prevent the app from locking?

Any other comments to my class is welcomed, since i'm new to .Net.



Regards /Snedker
 
C

Chris

You need to do the download in a seperate thread from the form.

Dim DF as new DownloadFile Class
Dim t As System.Threading.Thread
t = New System.Threading.Thread(AddressOf DF.DownloadFile)
t.Start()

Now create a property in your DownloadFile class that returns the progress
amount. Do a loop in the form something like

Do While DF.CurrentProgressPercent < 100
'Update Progress Bar in here
'Now sleep for a short time
System.Threading.Thread.CurrentThread.Sleep(500) 'I think
that's close to the class that you need, don't have ide on this puter
Loop
'Close the thread
t.abort()

That's the idea. Syntax might not be perfect cause I wrote it on the fly.
Chris
 
M

Morten Snedker

You need to do the download in a seperate thread from the form.

Dim DF as new DownloadFile Class
Dim t As System.Threading.Thread
t = New System.Threading.Thread(AddressOf DF.DownloadFile)
t.Start()

Now create a property in your DownloadFile class that returns the progress
amount. Do a loop in the form something like

Do While DF.CurrentProgressPercent < 100
'Update Progress Bar in here
'Now sleep for a short time
System.Threading.Thread.CurrentThread.Sleep(500) 'I think
that's close to the class that you need, don't have ide on this puter
Loop
'Close the thread
t.abort()

First of all, thx for your reply. I tried your suggestion - the code
works, but it still locks my application. I start the download, but it
is as if it doesn't reach my loop until the download has finished.

This is the code I have:

Dim l As Long
Dim t As System.Threading.Thread
t = New System.Threading.Thread(AddressOf df.DownloadFile)
t.Start()

'Timer1.Start()

df.FileDestination = "F:\administrations3.adp"
df.FileSource = "http://dbconsult.dk/AdministrationS3.adp"

l = df.getURLFileSize()

If l = 0 Then
MsgBox("Fil-størrelse kunne ikke findes")
Else
df.DownloadFile()
End If

Do While df.PercentCompleted < 100
System.Threading.Thread.Sleep(500)
Me.ListBox1.Items.Add(df.PercentCompleted)
Loop

t.Abort()
df = Nothing


Regards /Snedker
 
M

Morten Snedker

Well, I had actually sent you a link to the source of WTR, which
contains a reusable async download class. But maybe you didn't notice it,
so have this instead!

I didn't notice in the first place. Thanks - your help is greatly
appreciated (and made me understand better).

Also thanks to Chris for input and inspiration.

Regards /Snedker
 
M

Morten Snedker

Hi there,

Well, I had actually sent you a link to the source of WTR, which
contains a reusable async download class. But maybe you didn't notice it,
so have this instead!

Nick.

Just want to say that I've added

With pWrqRequest
.Proxy = WebProxy.GetDefaultProxy
.Proxy.Credentials = CredentialCache.DefaultCredentials()

to the dostart procedure. This helped me out with 403/407 error from
the ISA/proxy server.


Regards /Snedker
 
N

Nak

Hi there,

Aah, I hadn't even thought of proxy configuration, good idea, cheers for
sharing that! :)

Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top