B
Broggy69
Guest
Downloading files in an array.
I created an array of files I need to download. However when I run the code I get System.NotSupportedException: WebClient does not support concurrent I/O operations.
However I added:
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
And running in a for loop.
If I add a message box I can wait for file to download and then click OK, but I do not believe I should have to do that.
Here is the code I have:
Imports System.Net
Imports System.ComponentModel
Public Class Form1
Dim WithEvents WClient As New WebClient
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ftpsite As String = "ftp://mysite.com/"
'*** For Third Party Files ***
Dim DB_3rd_FTPFiles() As String = {"File1.zip",
"File2.exe",
File3.zip",
File4.zip"}
For Each ThirdFTP As String In DB_3rd_FTPFiles
If ThirdFTP = "File2.zip" Then
WClient.Credentials = New NetworkCredential("user", "Password")
Dim Downloadpath2 As New Uri(ftpsite & "Folder2/" & ThirdFTP)
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
Try
WClient.DownloadFileAsync(Downloadpath2, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'WClient.Dispose() 'I tried this no change
'MsgBox("Wait for " & ThirdFTP & " to be downloaded") 'When using this message box and wait until I see file downloads then it works.
End If
WClient.Credentials = New NetworkCredential("user", "password")
Dim Downloadpath As New Uri(ftpsite & ThirdFTP)
AddHandler WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
Try
WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
' WClient.Dispose()
'MsgBox("Wait for " & ThirdFTP & " to be downloaded")
Next
End Sub
Public Sub WC_DownloadComplete(sender As Object, e As AsyncCompletedEventArgs)
Using webClient As New Net.WebClient
RemoveHandler webClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
End Using
End Sub
Private Sub WClient_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WClient.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
'Progress bar only updates after file has been downloaded
End Sub
End Class
Any help is appreciated
Continue reading...
I created an array of files I need to download. However when I run the code I get System.NotSupportedException: WebClient does not support concurrent I/O operations.
However I added:
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
And running in a for loop.
If I add a message box I can wait for file to download and then click OK, but I do not believe I should have to do that.
Here is the code I have:
Imports System.Net
Imports System.ComponentModel
Public Class Form1
Dim WithEvents WClient As New WebClient
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ftpsite As String = "ftp://mysite.com/"
'*** For Third Party Files ***
Dim DB_3rd_FTPFiles() As String = {"File1.zip",
"File2.exe",
File3.zip",
File4.zip"}
For Each ThirdFTP As String In DB_3rd_FTPFiles
If ThirdFTP = "File2.zip" Then
WClient.Credentials = New NetworkCredential("user", "Password")
Dim Downloadpath2 As New Uri(ftpsite & "Folder2/" & ThirdFTP)
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
Try
WClient.DownloadFileAsync(Downloadpath2, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'WClient.Dispose() 'I tried this no change
'MsgBox("Wait for " & ThirdFTP & " to be downloaded") 'When using this message box and wait until I see file downloads then it works.
End If
WClient.Credentials = New NetworkCredential("user", "password")
Dim Downloadpath As New Uri(ftpsite & ThirdFTP)
AddHandler WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
Try
WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\3rdParty\" & ThirdFTP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
' WClient.Dispose()
'MsgBox("Wait for " & ThirdFTP & " to be downloaded")
Next
End Sub
Public Sub WC_DownloadComplete(sender As Object, e As AsyncCompletedEventArgs)
Using webClient As New Net.WebClient
RemoveHandler webClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
End Using
End Sub
Private Sub WClient_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WClient.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
'Progress bar only updates after file has been downloaded
End Sub
End Class
Any help is appreciated
Continue reading...