Hello... I'm working on an application that contains a "BackgroundWorker", the "BackgroundWorker" worked fine but after I started to change some stuff it gives me an error ("Exception has been thrown by the target of an invocation.")... I used a breakpoint to see where it happens, the exception gets thrown at the "End Sub" line of the BackgroundWorker... I then deleted all the code inside of it and ran it again... I still get the error.

-Thanks

You get an error running this code?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

End Sub

You get an error running this code?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

End Sub

Yeah... It an exception thou...

Stumps Me? Must be more to it behind the scenes.

Maybe post some code. The above code should not throw an exception. I would say add a Try Catch Block in the code to see whats wrong. But theres no code!!!

Stumps Me? Must be more to it behind the scenes.

Maybe post some code. The above code should not throw an exception. I would say add a Try Catch Block in the code to see whats wrong. But theres no code!!!

The application gets all the emails and relative links from a webpage, here's ALL the code:

Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Imports System.Net


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim EList As String
    Dim numE As Integer = 0
    Dim LinkList As String
    Dim url As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
        If URLs.Text.StartsWith("http://www.") Then
            url = URLs.Text
            BackgroundWorker1.RunWorkerAsync()
        Else
            MsgBox("Ivalid URL!")
        End If


    End Sub
    Private Function ExtractEmailAddressesFromString(ByVal source As String) As String()
        Dim mc As MatchCollection
        Dim i As Integer
        mc = Regex.Matches(source, "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})")
        Dim results(mc.Count - 1) As String
        For i = 0 To results.Length - 1
            results(i) = mc(i).Value
        Next

        Return results
    End Function

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        'BackgroundWorker1.ReportProgress(1)


        'Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)

        'BackgroundWorker1.ReportProgress(10)

        'Dim response As System.Net.HttpWebResponse = request.GetResponse
        'BackgroundWorker1.ReportProgress(20)
        'Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
        'BackgroundWorker1.ReportProgress(30)
        'Dim rssourcecode As String = sr.ReadToEnd
        'BackgroundWorker1.ReportProgress(70)


        'Dim emails As String()
        'Dim email As String
        'Dim results As New StringBuilder

        'emails = ExtractEmailAddressesFromString(rssourcecode)
        'BackgroundWorker1.ReportProgress(90)

        'For Each email In emails
        'EList = EList & email & " "

        'Next
        'BackgroundWorker1.ReportProgress(100)
        '///

        'Dim Links As String()
        'Dim Link As String
        'Dim LinkResults As New StringBuilder

        'Links = ExtractLinksFromString(rssourcecode)
        'BackgroundWorker1.ReportProgress(90)

        'For Each Link In Links
        'LinkList = LinkList & Link & " "
        'BackgroundWorker1.ReportProgress(100)
        'Next


    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar.Value = e.ProgressPercentage
        Dim iteg As Integer = ProgressBar.Value
        Select Case iteg
            Case 1
                LOG.Text = LOG.Text + vbNewLine + "Getting HTML code..."
            Case 30
                LOG.Text = LOG.Text + vbNewLine + "Got HTML code."
                LOG.Text = LOG.Text + vbNewLine + "Reading HTML code..."
            Case 70
                LOG.Text = LOG.Text + vbNewLine + "Done reading."
                LOG.Text = LOG.Text + vbNewLine + "Finding emails..."

            Case 90
                LOG.Text = LOG.Text + vbNewLine + "Found emails." + vbNewLine + "Processing emails"
        End Select
    End Sub


    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        EList = EList.Substring(0, EList.Length - 1)
        Dim ESplitList As String() = EList.Split(New Char() {" "c})
        Dim email As String
        For Each email In ESplitList

            If Not EmailList.Items.Contains(email) = True Then
                numE += 1
                EmailList.Items.Add(email)
            End If
            Next
        LOG.Text = LOG.Text + vbNewLine + "Done!" + vbNewLine + numE.ToString + " new emails added to the list."
        numE = 0
        EList = ""
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub LOG_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LOG.TextChanged
        LOG.SelectionStart = LOG.Text.Length

        LOG.ScrollToCaret()
    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        SaveForm.Show()
    End Sub
    Private Function ExtractLinksFromString(ByVal source As String) As String()
        Dim mc As MatchCollection
        Dim i As Integer
        mc = Regex.Matches(source, "href=(['""])(?!.+://)(?<url>.+?)\1")
        Dim results(mc.Count - 1) As String
        For i = 0 To results.Length - 1
            results(i) = mc(i).Value
        Next

        Return results
    End Function
End Class

Like i said at first: I put "'"s before all the code in the BackgroundWorker's code.

Edited 4 Years Ago by AndyPants: n/a

I figured there was more behind the scenes.

Your problem is in the run worker complete part. I don't know what it is. I will look at it later. Add a try catch block in your runworkercomplete sub and put a break point on the catch part or message box the exception. You will probably figure it out. If not post back.

I did a quick look and I will be willing to wager $0.50 that your elist variable is an empty string and your trying to get a sub string from it.

Let me know if any of this information is not correct.

Everything about the above post is wrong. The ProgressChanged event is not even being raised because there is no code in the DoWork event that sets a change. And even if the ProgressChanged was raised you CAN access controls in it.

I've already provided the solutions to the problem.

I did a quick look and I will be willing to wager $0.50 that your elist variable is an empty string and your trying to get a sub string from it.

Let me know if any of this information is not correct.

I deleted the first line of code in BackgroundWorker1_RunWorkerCompleted... nothing changed...

I deleted the first line of code in BackgroundWorker1_RunWorkerCompleted... nothing changed.

You don't fix code by deleting other lines of code.

If the string is null then your going to get an exception on the second line.

Too many other factors in this to even do anything. Wheres EmailList? Its not in your form1 class. Is it in some module or other class?

You may need to just start over.

EmailList may not even have a reference.

Everything about the above post is wrong. The ProgressChanged event is not even being raised because there is no code in the DoWork event that sets a change. And even if the ProgressChanged was raised you CAN access controls in it.

When I say there is no code in the DoWork event that sets a change. I mean its all commented out. Well aware there is commented out code that would set a progress change.

@AndyPants

You should start validating things before using them or you will be dealing with things like this from here on.

Before substring a string. If not String.IsNullOrEmpty(aString) andalso aString.Length > 0 then

Before using a list or what ever EmailList is. If EmailList IsNot Nothing Then.

Before setting the minimum or maximum of a progress bar. If value >= progressbar.minimum AndAlso value <= progressbar.maximum then.


And you should put a Try Catch Block in the entire RunWorkerComplete and set a break point on the Catch part so you can read what the exception is.


Good Luck and Happy Trails.

Edited 4 Years Ago by Unhnd_Exception: n/a

I deleted the first line of code in BackgroundWorker1_RunWorkerCompleted... nothing changed.

Unbelievable. I'll try that next time I get an error. Just delete a line. Great strategy. I think its great. Damn it its not working. I'll just delete this line and see what happens. When all else fails, just delete a line of code.

@GeekByCoiCe Your post is as worthless as this thread.

This question has already been answered. Start a new discussion instead.