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.
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.