WebClient with download progress causes targetinvocationexception...

SuperSaiyanZero

New member
Joined
Jun 5, 2006
Messages
2
Programming Experience
Beginner
Hello,
I'm new to visual basic since I come from a Delphi background. I've got a problem which I don't how to solve in VB .NET. I get an error message of TargetInvocationException when I assign AddHandler to the WebClient() for showing the download progress.

Sample Code:

Private Sub DownloadAsync(ByVal URL As String)
Dim wc As New WebClient()
Dim Address As New Uri(URL)
AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgress
AddHandler wc.DownloadFileCompleted, AddressOf DownloadComplete
If (URL.Contains("?") = True) Then
URL.Remove(URL.LastIndexOf("?"), URL.Length)
End If
SaveFileDialogue.DefaultExt = URL.Substring(URL.LastIndexOf("."))
SaveFileDialogue.Filter =
"." + UCase(SaveFileDialogue.DefaultExt) + " file type (*." + SaveFileDialogue.DefaultExt + ")|*" + SaveFileDialogue.DefaultExt
SaveFileDialogue.ShowDialog()
If (SaveFileDialogue.FileName <> "") Then
'Try
wc.DownloadFileAsync(Address, SaveFileDialogue.FileName)
'Catch ex As Reflection.TargetInvocationException
'EventLog.WriteEntry(ex.ToString())
'EventLog.Close()
'MessageBox.Show("Failed to initialise download!", "Download Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error)
'End Try
End If
End Sub

Private Shared Sub DownloadProgress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
MainForm.TheProgressBar.Value = e.ProgressPercentage
MainForm.PercentageLabel.Text =
CStr(CInt(e.BytesReceived / 1024) + " / " + CInt(e.TotalBytesToReceive / 1024) + " KB, " + CStr(e.ProgressPercentage))
End Sub

If I comment the addhandler for the download progress or comment all the lines out in the downloadprogress sub then everything works fine and I'm able to download the file. Even the download complete handler works and alerts me that the download has finished.

DownloadComplete sample Code:
Private Shared Sub DownloadComplete(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
MessageBox.Show(
"Your download is complete!", "Download Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Please help since I'm new at Visual Basic/Visual Studio.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
Why have you declared these sub methods shared?? Also accessing the controls looks suspicious, IE "Mainform.TheProgres...". Is Mainform really an instance? Try:
VB.NET:
[COLOR=#0000ff]Private [/COLOR][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DownloadProgress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DownloadProgressChangedEventArgs)
[/SIZE][SIZE=2]TheProgressBar.Value = e.ProgressPercentage
PercentageLabel.Text = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](e.BytesReceived / 1024) + [/SIZE][SIZE=2][COLOR=#800000]" / "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](e.TotalBytesToReceive / 1024) + [/SIZE][SIZE=2][COLOR=#800000]" KB, "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](e.ProgressPercentage))
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
This works when this method is used like this within the form class.
 

SuperSaiyanZero

New member
Joined
Jun 5, 2006
Messages
2
Programming Experience
Beginner
TargetInvocation Fixed!

JohnH said:
Why have you declared these sub methods shared?? Also accessing the controls looks suspicious, IE "Mainform.TheProgres...". Is Mainform really an instance? Try:
VB.NET:
[COLOR=#0000ff]Private [/COLOR][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DownloadProgress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DownloadProgressChangedEventArgs)
[/SIZE][SIZE=2]TheProgressBar.Value = e.ProgressPercentage
PercentageLabel.Text = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](e.BytesReceived / 1024) + [/SIZE][SIZE=2][COLOR=#800000]" / "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](e.TotalBytesToReceive / 1024) + [/SIZE][SIZE=2][COLOR=#800000]" KB, "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](e.ProgressPercentage))
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
This works when this method is used like this within the form class.

I've declared them as shared because of the sample microsoft posted on MSDN! I've managed to solve the problem, it was with the DownloadProgress Sub and thanks to a lot of help from the MSDN forums. The corrected sub looks like:

PrivateSharedSub DownloadProgress(ByVal sender AsObject, ByVal e As DownloadProgressChangedEventArgs)
MainForm.TheProgressBar.Value = e.ProgressPercentage
MainForm.TheProgressBar.Refresh()
MainForm.PercentageLabel.Text =
CStr(CInt(e.BytesReceived / 1024)) + " of " + CStr(CInt(e.TotalBytesToReceive / 1024)) + " KB, " + CStr(e.ProgressPercentage)
MainForm.PercentageLabel.Refresh()
EndSub

With the new sub, the programme works perfectly well! Thank you for your reply! I will give your suggestion a try, though!
 
Top Bottom