user3516240
user3516240

Reputation: 375

Background worker Exception Thrown By Invocation in the RunWorkerCompleted Event

Okay, I have BackgroundWorker1 downloading a series of files to configure my database with, this does work under the normal form load event, and it is working well in the backgroundworker - I used a message box to display the string's contents and they were correct.

But I'm receiving an error:

System.Reflection.TargetInvocationException occurred Message: A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of an invocation.

In my BackgroundWorker1_RunWorkerCompleted- which is a little strange to me as I'm a self-taught coder and have only ever had this issue in the .DoWork.

Here's the relevant code:

Public Function GetRemoteFile(ByVal url As String) As String
        ' Create web request, and get its response.
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        ' Read the content from the stream.
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim content As String = reader.ReadToEnd()
        ' Dispose of our managed types.
        reader.Dispose()
        Return content
    End Function

Public Class MyParameters
            Public _ServerIP As String
            Public _DatabaseUserName As String
            Public _RegisterKey As String
            Public _AccessCode As String
            Public _TargetUGPoints As String
        End Class


Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim args As New MyParameters
        BackgroundWorker1.RunWorkerAsync(args)
    End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim args As MyParameters = DirectCast(e.Argument, MyParameters) 'Convert the generic Object back into a MyParameters object

        'Ive removed the URLs from my code here for obvious reasons.
        args._ServerIP = GetRemoteFile("https://")
        args._DatabaseUserName = GetRemoteFile("https://")
        args._ServerIP = GetRemoteFile("https://")
        args._DatabaseUserName = GetRemoteFile("https://")
        args._RegisterKey = GetRemoteFile("https://")
        args._AccessCode = GetRemoteFile("https://")
        args._TargetUGPoints = GetRemoteFile("https://")
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ' Called when the BackgroundWorker is completed.
        Dim args As MyParameters = DirectCast(e.Result, MyParameters) 'Convert the generic Object back into a MyParameters object

        serveriptxt.Text = args._ServerIP.ToString
        databaseusertxt.Text = args._DatabaseUserName.ToString
        My.Settings.ServerIP = args._ServerIP.ToString.ToString
        My.Settings.UserName = args._DatabaseUserName.ToString
        My.Settings.RegisterKey = args._RegisterKey.ToString
        My.Settings.AccessCode = args._AccessCode.ToString
        My.Settings.TargetUGPoints = args._TargetUGPoints.ToString
        My.Settings.Save()
        TabControl1.Visible = True

    End Sub

Can you see why I'm receiving this error? As I said, the background worker itself does complete (or reached the end of the sub), it's only at the WorkerCompleted event I receive the error, and it doesn't highlight the line the error's on.

Upvotes: 2

Views: 869

Answers (1)

To go along with my comment, I think you need to assign e.Result a value in the DoWork method. It's erroring out because you are trying to access something that doesn't exist in the RunWorkerCompleted method. Try this:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim args As MyParameters = DirectCast(e.Argument, MyParameters) 'Convert the generic Object back into a MyParameters object

    'Ive removed the URLs from my code here for obvious reasons.
    args._ServerIP = GetRemoteFile("https://")
    args._DatabaseUserName = GetRemoteFile("https://")
    args._ServerIP = GetRemoteFile("https://")
    args._DatabaseUserName = GetRemoteFile("https://")
    args._RegisterKey = GetRemoteFile("https://")
    args._AccessCode = GetRemoteFile("https://")
    args._TargetUGPoints = GetRemoteFile("https://")

    e.Result = args

End Sub

Of course I could be wrong, I don't have access to an IDE at this time to test with.

Upvotes: 1

Related Questions