download using webclient with downloadasync  
Author Message
SuperSaiyanZero





PostPosted: .NET Framework Networking and Communication, download using webclient with downloadasync Top

Hello,

I'm currently programming in VB .NET 2005. I've got a working programme which downloads and saves the file to the location I choose but I have one problem.

If I download using DownloadFileAsync method of the webclient, I receive feedback on how much has been downloaded but I can't seem to queue another file as it fails the first file and begins the next. The only way to pause between the calls to the method DownloadAsync is to insert a messagebox.

Now I can solve this problem using the method DownloadFile of the webclient but then I don't get any feedback of how much of the file has been downloaded.

the method which calls the downloadasync,

Private Sub DownloadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadButton.Click

Dim ShowDialogue As Boolean = True

DownloadAsync(OnlineFileRef, ShowDialogue)

'MessageBox.Show("Download 1", "DownloadAsync", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

DownloadAsync(OnlineFileRef, False)

'DownloadFile(OnlineFileRef, ShowDialogue)

'DownloadFile(OnlineFileRef, False)

End Sub

the downloadasync method,

Private Sub DownloadAsync(ByVal URL As String, ByVal ShowDialogue As Boolean)

CurrentFile = CurrentFile + 1

Dim ZIPIndex As Integer = CurrentFile - 1

'AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgress

'AddHandler wc.DownloadFileCompleted, AddressOf DownloadComplete

Try

If ShowDialogue = True Then

SaveFileDialogue.DefaultExt = URL.Substring(URL.LastIndexOf("."))

SaveFileDialogue.Filter = UCase(SaveFileDialogue.DefaultExt) + " File Type (*." + SaveFileDialogue.DefaultExt + ")|*" + SaveFileDialogue.DefaultExt

SaveFileDialogue.ShowDialog()

Else

URL = URL.Remove(URL.Length - 2, 2)

URL += "0" + ZIPIndex.ToString()

SaveFileDialogue.FileName = SaveFileDialogue.FileName.Substring(0, SaveFileDialogue.FileName.LastIndexOf(".")) + URL.Substring(URL.LastIndexOf("."))

End If

Dim Address As New Uri(URL)

If (SaveFileDialogue.FileName <> "") Then

DownloadButton.Enabled = False

CancelDownloadButton.Enabled = True

If (wc.IsBusy = True) Then

wc.Dispose()

End If

wc.DownloadFileAsync(Address, SaveFileDialogue.FileName)

Else

CancelDownloadButton.Enabled = False

DownloadButton.Enabled = True

End If

Catch ex As Exception

SendMail("(DownloadAsync)", ex.ToString())

MessageBox.Show("Failed to completed download!", "Download failed", MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally

wc.Dispose()

CancelDownloadButton.Enabled = False

DownloadButton.Enabled = True

End Try

End Sub

Any Ideas Thanks for the help in advance!




.NET Development30  
 
 
JonCole - MSFT





PostPosted: .NET Framework Networking and Communication, download using webclient with downloadasync Top

Looks like you are doing a couple of things I don't understand.

First, in your DownloadButton_Click sub routine, you are calling DownloadFileAsync twice. You can only have one outstanding async operation on a single instance of WebClient at a time. In other words, you have to wait for one async operaiton to complete before starting the next. By calling that routine twice within a short period of time you a race condition where you periodically cause errors by trying to start a second async operation on the web client before the first operation is complete.

Second, Inside your DownloadFileAsync routine, you have "if (wc.IsBusy = true) then...wc.dispose()" This could also be the cause of your file download being canceled.



 
 
SuperSaiyanZero





PostPosted: .NET Framework Networking and Communication, download using webclient with downloadasync Top

Hello,

Thanks for the reply! I've got the downlaodasync because I want to download 2 files. The files are zip files on my server and I need the end users to be able to download the files without having to type in the url or make any modifications to the programme.

Most of them are 2 zip files (example.zip and example.z01).

wc.IsBusy was used as a test to see if the webclient is busy then terminate it.

I have implemented the solution to this problem but it's not my preferred one, if there's a better solution then please let me know.

My solution is:
Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick
If wc.IsBusy = False Then
DownloadAsync(OnlineFileRef, False)
Else
DownloadButton.Enabled = False
CancelDownloadButton.Enabled = True
End If
If CurrentFile = TotalFiles Then
Clock.Enabled = False
End If
End Sub

I've set a timer which checks if the webclient is busy if it is then disable the buttons else it will make a call to downloadasync to download the second part of the zip file.


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, download using webclient with downloadasync Top

You cannont download more than one files using a single WebClient at the same time. To download 2 files simultainously, you need 2 instances of WebClient class!

Best Regards,



 
 
SuperSaiyanZero





PostPosted: .NET Framework Networking and Communication, download using webclient with downloadasync Top

Thanks for your reply,

I know you can only have a single instance of DownloadAsync running from a single WebClient class.

I've managed to do what I wanted, although it's not a perfect solution but it works fine!

Thank you for your help!