We help IT Professionals succeed at work.

vb.net background worker crossthread

PeterBaileyUk
on
374 Views
Last Modified: 2017-04-14
I have the following that I want to be done as a background process. it fails with crossthread error understandably  on the
  StrTemp = CBList.Text

Open in new window


I know why as the cblist is part of the GUI and the background process is not but I dont know how to get around that. ive used the background worker in another part of the program but that's fine as it doesnt rely on a value from a cb whereas this does.

the background task
 Private Sub CBList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBList.SelectedIndexChanged
        'adds common word selected to link table
        Dim deletions As New List(Of String)
        Dim StrCommonSet As Boolean
        Dim StrTemp As String
        StrTemp = ""
        Dim y As Integer
        StrCommonSet = False

        If OPBulkDescView4.Checked = True Then
            Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
                cnSql.Open()
                y = DataGridViewStringsBulk.RowCount
                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If Not Convert.IsDBNull(row.Cells("StrCommon").Value) Then

                        If Not deletions.Contains(row.Cells("StrCommon").Value, StringComparer.OrdinalIgnoreCase) Then
                            deletions.Add(row.Cells("StrCommon").Value)
                        End If

                    End If
                Next

                For Each term In deletions
                    Using cmdInsert As New SqlCommand("usp_DeleteShortDescLinkWords", cnSql)
                        cmdInsert.CommandType = CommandType.StoredProcedure
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.Parameters.AddWithValue("@Word", term)
                        cmdInsert.ExecuteScalar()
                    End Using
                Next

                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If StrCommonSet = False Then
                        StrTemp = CBList.Text
                        StrCommonSet = True
                    End If

                    Using cmdInsert As New SqlClient.SqlCommand
                        cmdInsert.Connection = cnSql
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.CommandText = "INSERT INTO TblShortDescLink (StrShort, StrCommon) VALUES ('" & row.Cells("Strshort").Value & "','" & StrTemp & "'" & ")"
                        cmdInsert.ExecuteNonQuery()
                    End Using
                Next
                Using cmdCommonInsert As New SqlCommand("usp_UpdateStrCommon", cnSql)
                    cmdCommonInsert.CommandType = CommandType.StoredProcedure
                    cmdCommonInsert.CommandTimeout = 0
                    cmdCommonInsert.ExecuteNonQuery()
                End Using
            End Using

            'MessageBox.Show("Linking finished")
        Else
            'MessageBox.Show("StrShort must be checked in order to link succesfully")
        End If
    End Sub

Open in new window


background worker 3 code
    Private Sub startAsyncButton3_Click(ByVal sender As System.Object,
    ByVal e3 As System.EventArgs) Handles CBList.SelectedIndexChanged


        If BackgroundWorker3.IsBusy <> True Then
            ' Start the asynchronous operation.

            Me.ResultLabelLink.Visible = True
            Me.ResultLabelLink.Text = "Processing Link Words"
            Me.ProgressBar4.Visible = True
            Me.ProgressBar4.Enabled = True
            'Me.CBList.Enabled = False

            'Me.StrShortCancel.Enabled = True

            BackgroundWorker3.RunWorkerAsync()
        End If
    End Sub


    Private Sub backgroundWorker3_DoWork(ByVal sender As System.Object,
    ByVal e3 As DoWorkEventArgs) Handles BackgroundWorker3.DoWork
        Dim worker3 As BackgroundWorker = CType(sender, BackgroundWorker)

        ' Perform a time consuming operation and report progress.
        'worker3.ReportProgress(30)


    End Sub

    Private Sub backgroundWorker3_ProgressChanged(ByVal sender As System.Object,
    ByVal e3 As ProgressChangedEventArgs) Handles BackgroundWorker3.ProgressChanged

        'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
        Me.ProgressBar4.Enabled = True
        Me.ProgressBar4.Visible = True

    End Sub


    Private Sub backgroundWorker3_RunWorkerCompleted(ByVal sender As System.Object,
    ByVal e3 As RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted


        If e3.Cancelled = True Then
            ResultLabelLink.Text = "Canceled!"

        ElseIf e3.Error IsNot Nothing Then
            ResultLabelLink.Text = "Error: " & e3.Error.Message
        Else

            DataGroupCallStringsBulk()

            Me.ProgressBar4.Enabled = False
            Me.ProgressBar4.Visible = False
            Me.CBList.Enabled = True

            ResultLabelLink.Visible = False
            DataGridViewStringsBulk.Refresh()

        End If
    End Sub

Open in new window

Comment
Watch Question

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Just to clarify, you want the selected index code to be a part of the background worker do work method?

-saige-

Author

Commented:
yes this:

 'adds common word selected to link table
        Dim deletions As New List(Of String)
        Dim StrCommonSet As Boolean
        Dim StrTemp As String
        StrTemp = ""
        Dim y As Integer
        StrCommonSet = False

        If OPBulkDescView4.Checked = True Then
            Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
                cnSql.Open()
                y = DataGridViewStringsBulk.RowCount
                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If Not Convert.IsDBNull(row.Cells("StrCommon").Value) Then

                        If Not deletions.Contains(row.Cells("StrCommon").Value, StringComparer.OrdinalIgnoreCase) Then
                            deletions.Add(row.Cells("StrCommon").Value)
                        End If

                    End If
                Next

                For Each term In deletions
                    Using cmdInsert As New SqlCommand("usp_DeleteShortDescLinkWords", cnSql)
                        cmdInsert.CommandType = CommandType.StoredProcedure
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.Parameters.AddWithValue("@Word", term)
                        cmdInsert.ExecuteScalar()
                    End Using
                Next

                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If StrCommonSet = False Then
                        StrTemp = CBList.Text
                        StrCommonSet = True
                    End If

                    Using cmdInsert As New SqlClient.SqlCommand
                        cmdInsert.Connection = cnSql
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.CommandText = "INSERT INTO TblShortDescLink (StrShort, StrCommon) VALUES ('" & row.Cells("Strshort").Value & "','" & StrTemp & "'" & ")"
                        cmdInsert.ExecuteNonQuery()
                    End Using
                Next
                Using cmdCommonInsert As New SqlCommand("usp_UpdateStrCommon", cnSql)
                    cmdCommonInsert.CommandType = CommandType.StoredProcedure
                    cmdCommonInsert.CommandTimeout = 0
                    cmdCommonInsert.ExecuteNonQuery()
                End Using
            End Using

            'MessageBox.Show("Linking finished")
        Else
            'MessageBox.Show("StrShort must be checked in order to link succesfully")
        End If

Open in new window

CERTIFIED EXPERT

Commented:
Change this section:
If StrCommonSet = False Then
'StrTemp = CBList.Text
StrTemp = bg_GetCBlistText
StrCommonSet = True
End If

Open in new window


Add this function outside of the Background Job
Private Delegate Function bg_GetCBListTextInvoker()
    Private Function bg_GetCBListText()
        If Me.CBList.InvokeRequired Then
            Me.CBList.Invoke(New bg_GetCBListTextInvoker(AddressOf bg_GetCBListText))
            Return CBList.Text
        Else
            Return CBList.Text
        End If
    End Function

Open in new window

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Normally I would say that you want to send the text as a parameter to the worker, but it appears as you need to actually send more than this, you also have other controls in this method (OPBulkDescView4 and DataGridViewStringBulk).  As you are only viewing these then you could still use a parameter list; e.g. (untested) -
Private Sub startAsyncButton3_Click(ByVal sender As Object, ByVal e3 As EventArgs) Handles CBList.SelectedIndexChanged
    If BackgroundWorker3.IsBusy <> True Then
        ' Start the asynchronous operation.

        Me.ResultLabelLink.Visible = True
        Me.ResultLabelLink.Text = "Processing Link Words"
        Me.ProgressBar4.Visible = True
        Me.ProgressBar4.Enabled = True
        'Me.CBList.Enabled = False

        'Me.StrShortCancel.Enabled = True

        BackgroundWorker3.RunWorkerAsync(New With {CBList.Text, OPBulkDescView4.Checked, DataGridViewStringsBulk.RowCount, .SelectedRows = New List(Of DataGridViewRow)(DataGridViewStringsBulk.SelectedRows.Cast(Of DataGridViewRow)})
    End If
End Sub


Private Sub backgroundWorker3_DoWork(ByVal sender As Object, ByVal e3 As DoWorkEventArgs) Handles BackgroundWorker3.DoWork
    Dim worker = DirectCast(sender, BackgroundWorker)
    Dim deletions As New List(Of String)
    Dim StrCommonSet As Boolean
    Dim StrTemp As String = ""
    Dim Viewed As Boolean = False
    Dim RowCount As Integer = 0
    Dim SelectedRows As New List(Of DataGridViewRow)()
    Dim y As Integer
    If e3.Argument IsNot Nothing Then
        StrTemp = If(e3.Argument(0) IsNot Nothing, Convert.ToString(e3.Argument(0)), "")
        Viewed = If(e3.Argument(1) IsNot Nothing, Convert.ToBoolean(e3.Argument(1)), False)
        RowCount = If(e3.Argument(2) IsNot Nothing, Convert.ToInt64(e3.Argument(2)), 0)
        SelectedRows = If(e3.Argument(3) IsNot Nothing, DirectCast(e3.Argument(3), List(Of DataGridViewRow)), Nothing)
    End If
    'adds common word selected to link table
    StrCommonSet = False

    If Viewed Then
        Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
            cnSql.Open()
            y = RowCount
            For Each row In SelectedRows
                If Not Convert.IsDBNull(row.Cells("StrCommon").Value) Then
                    If Not deletions.Contains(row.Cells("StrCommon").Value, StringComparer.OrdinalIgnoreCase) Then
                        deletions.Add(row.Cells("StrCommon").Value)
                    End If

                End If
            Next

            For Each term In deletions
                Using cmdInsert As New SqlCommand("usp_DeleteShortDescLinkWords", cnSql)
                    cmdInsert.CommandType = CommandType.StoredProcedure
                    cmdInsert.CommandTimeout = 0
                    cmdInsert.Parameters.AddWithValue("@Word", term)
                    cmdInsert.ExecuteScalar()
                End Using
            Next

            For Each row In SelectedRows
                If StrCommonSet = False Then
                    StrCommonSet = True
                End If

                Using cmdInsert As New SqlClient.SqlCommand
                    cmdInsert.Connection = cnSql
                    cmdInsert.CommandTimeout = 0
                    cmdInsert.CommandText = "INSERT INTO TblShortDescLink (StrShort, StrCommon) VALUES ('" & row.Cells("Strshort").Value & "','" & StrTemp & "'" & ")"
                    cmdInsert.ExecuteNonQuery()
                End Using
            Next
            Using cmdCommonInsert As New SqlCommand("usp_UpdateStrCommon", cnSql)
                cmdCommonInsert.CommandType = CommandType.StoredProcedure
                cmdCommonInsert.CommandTimeout = 0
                cmdCommonInsert.ExecuteNonQuery()
            End Using
        End Using

        'MessageBox.Show("Linking finished")
    Else
        'MessageBox.Show("StrShort must be checked in order to link succesfully")
    End If
End Sub

Private Sub backgroundWorker3_ProgressChanged(ByVal sender As Object, ByVal e3 As ProgressChangedEventArgs) Handles BackgroundWorker3.ProgressChanged

    'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
    Me.ProgressBar4.Enabled = True
    Me.ProgressBar4.Visible = True

End Sub


Private Sub backgroundWorker3_RunWorkerCompleted(ByVal sender As Object, ByVal e3 As RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted


    If e3.Cancelled = True Then
        ResultLabelLink.Text = "Canceled!"

    ElseIf e3.Error IsNot Nothing Then
        ResultLabelLink.Text = "Error: " & e3.Error.Message
    Else

        DataGroupCallStringsBulk()

        Me.ProgressBar4.Enabled = False
        Me.ProgressBar4.Visible = False
        Me.CBList.Enabled = True

        ResultLabelLink.Visible = False
        DataGridViewStringsBulk.Refresh()

    End If
End Sub

Open in new window


-saige-

Author

Commented:
ok I started with ktaczala's solution which compiled but gave the cross thread message in the first part of the function, I tried IT saige's solution too and that gave syntax errors on

(DataGridViewStringsBulk.SelectedRows.Cast(Of DataGridViewRow)})

Open in new window

IT Saige syntax here


ktaczala's  failed here
Me.CBList.Invoke(New bg_GetCBListTextInvoker(AddressOf bg_GetCBListText))
            Return CBList.Text

Open in new window

failed here



Imports System
Imports System.Text.RegularExpressions
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Runtime.CompilerServices
Imports System.ComponentModel

Public Class Form1

    Dim AllListBox As New List(Of ListBox)()
    Private dtWords As DataTable

    'Private backgroundWorker1 As BackgroundWorker = New BackgroundWorker
    Public Sub New()
        InitializeComponent()

        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = False
        BackgroundWorker2.WorkerReportsProgress = True
        BackgroundWorker2.WorkerSupportsCancellation = False
        BackgroundWorker3.WorkerReportsProgress = True
        BackgroundWorker3.WorkerSupportsCancellation = False
    End Sub

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'StrShortCancel.Visible = False
        IsLoading = True

        Dim ctrl As Control = TabControl1
        'Me.StrShortCancel.Enabled = False
        ProgressBar2.Visible = False
        ProgressBar2.Enabled = False
        ProgressBar3.Visible = False
        ProgressBar3.Enabled = False
        ProgressBar4.Visible = False
        ProgressBar4.Enabled = False
        ResultLabel.Visible = False
        ResultLabelTag.Visible = False
        ResultLabelLink.Visible = False
        AllToolStripMenuItem.Checked = True
        CarsToolStripMenuItem.Checked = False
        MotorCyclesToolStripMenuItem.Checked = False
        LcvToolStripMenuItem.Checked = False
        OthersToolStripMenuItem.Checked = False
        CarLcvOtherToolStripMenuItem.Checked = False
        BikesQuadsToolStripMenuItem.Checked = False
        OPBulkDescView4.Checked = True
        StrShortStrTagClientVehTypeToolStripMenuItem.Checked = True
        OpBulkAllStrShort.Checked = True
        AllStrShortGroupsToolStripMenuItem.Checked = True
        OpShortDescAllAll.Checked = True
        AllToolStripMenuItem.Checked = True

        OpCreateStrWholeDb.Checked = True
        'WholeDBToolStripMenuItem.Checked = True
        OpBulkTag.Checked = True
        TagWordsToolStripMenuItem1.Checked = True
        OPIndividual.Checked = True
        IndividualToolStripMenuItem.Checked = True


        Me.TabControl1.SelectedTab = Me.TabPage6
        'populate right hand grid
        Call DataGroupCallStrCommon()
        IsLoading = False
        'only needed if start method 1

    End Sub
    Private Delegate Function bg_GetCBListTextInvoker()
    Private Function bg_GetCBListText()
        If Me.CBList.InvokeRequired Then
            Me.CBList.Invoke(New bg_GetCBListTextInvoker(AddressOf bg_GetCBListText))
            Return CBList.Text
        Else
            Return CBList.Text
        End If
    End Function
    Private Sub startAsyncButton_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles CreateShortStringsToolStripMenuItem.Click
        If BackgroundWorker1.IsBusy <> True Then
            ' Start the asynchronous operation.
            Me.ResultLabel.Visible = True
            Me.ResultLabel.Text = "Processing StrShort Strings"
            Me.ProgressBar2.Visible = True
            Me.ProgressBar2.Enabled = True
            Me.Panel1.Enabled = False
            Me.PanelBtnBulkTag.Enabled = False
            'Me.StrShortCancel.Enabled = True
            BackgroundWorker1.RunWorkerAsync()
        End If
    End Sub
    Private Sub startAsyncButton2_Click(ByVal sender As System.Object,
    ByVal e2 As System.EventArgs) Handles PanelBtnBulkTag.Click


        If BackgroundWorker2.IsBusy <> True Then
            ' Start the asynchronous operation.
            Me.ResultLabelTag.Visible = True
            Me.ResultLabelTag.Text = "Processing Tag Words"
            Me.ProgressBar3.Visible = True
            Me.ProgressBar3.Enabled = True
            Me.PanelBtnBulkTag.Enabled = False
            Me.Panel1.Enabled = False
            'Me.StrShortCancel.Enabled = True
            BackgroundWorker2.RunWorkerAsync()
        End If
    End Sub
    Private Sub startAsyncButton3_Click(ByVal sender As System.Object,
    ByVal e3 As System.EventArgs) Handles CBList.SelectedIndexChanged


        If BackgroundWorker3.IsBusy <> True Then
            ' Start the asynchronous operation.

            Me.ResultLabelLink.Visible = True
            Me.ResultLabelLink.Text = "Processing Link Words"
            Me.ProgressBar4.Visible = True
            Me.ProgressBar4.Enabled = True
            'Me.CBList.Enabled = False

            'Me.StrShortCancel.Enabled = True

            BackgroundWorker3.RunWorkerAsync()
        End If
    End Sub

    'This Event handler() Is where the time-consuming work Is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object,
    ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

        ' Perform a time consuming operation and report progress.
        'worker.ReportProgress(30)
        Call CreateAllStrs()

    End Sub
    Private Sub backgroundWorker2_DoWork(ByVal sender As System.Object,
    ByVal e2 As DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        Dim worker2 As BackgroundWorker = CType(sender, BackgroundWorker)

        ' Perform a time consuming operation and report progress.
        'worker.ReportProgress(30)

        Call TagBulkWords()

    End Sub

    Private Sub backgroundWorker3_DoWork(ByVal sender As System.Object,
    ByVal e3 As DoWorkEventArgs) Handles BackgroundWorker3.DoWork
        Dim worker3 As BackgroundWorker = CType(sender, BackgroundWorker)

        ' Perform a time consuming operation and report progress.
        'worker3.ReportProgress(30)
        'adds common word selected to link table
        Dim deletions As New List(Of String)
        Dim StrCommonSet As Boolean
        Dim StrTemp As String
        StrTemp = ""
        Dim y As Integer
        StrCommonSet = False

        If OPBulkDescView4.Checked = True Then
            Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
                cnSql.Open()
                y = DataGridViewStringsBulk.RowCount
                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If Not Convert.IsDBNull(row.Cells("StrCommon").Value) Then

                        If Not deletions.Contains(row.Cells("StrCommon").Value, StringComparer.OrdinalIgnoreCase) Then
                            deletions.Add(row.Cells("StrCommon").Value)
                        End If

                    End If
                Next

                For Each term In deletions
                    Using cmdInsert As New SqlCommand("usp_DeleteShortDescLinkWords", cnSql)
                        cmdInsert.CommandType = CommandType.StoredProcedure
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.Parameters.AddWithValue("@Word", term)
                        cmdInsert.ExecuteScalar()
                    End Using
                Next

                For Each row As DataGridViewRow In DataGridViewStringsBulk.SelectedRows

                    If StrCommonSet = False Then
                        'StrTemp = CBList.Text
                        StrTemp = bg_GetCBListText()
                        StrCommonSet = True
                    End If

                    Using cmdInsert As New SqlClient.SqlCommand
                        cmdInsert.Connection = cnSql
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.CommandText = "INSERT INTO TblShortDescLink (StrShort, StrCommon) VALUES ('" & row.Cells("Strshort").Value & "','" & StrTemp & "'" & ")"
                        cmdInsert.ExecuteNonQuery()
                    End Using
                Next
                Using cmdCommonInsert As New SqlCommand("usp_UpdateStrCommon", cnSql)
                    cmdCommonInsert.CommandType = CommandType.StoredProcedure
                    cmdCommonInsert.CommandTimeout = 0
                    cmdCommonInsert.ExecuteNonQuery()
                End Using
            End Using

            'MessageBox.Show("Linking finished")
        Else
            'MessageBox.Show("StrShort must be checked in order to link succesfully")
        End If

    End Sub

    'This Event handler() updates the progress.
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object,
    ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

        'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
        Me.ProgressBar2.Enabled = True
        Me.ProgressBar2.Visible = True

    End Sub
    Private Sub backgroundWorker2_ProgressChanged(ByVal sender As System.Object,
    ByVal e2 As ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged

        'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
        Me.ProgressBar3.Enabled = True
        Me.ProgressBar3.Visible = True

    End Sub
    Private Sub backgroundWorker3_ProgressChanged(ByVal sender As System.Object,
    ByVal e3 As ProgressChangedEventArgs) Handles BackgroundWorker3.ProgressChanged

        'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
        Me.ProgressBar4.Enabled = True
        Me.ProgressBar4.Visible = True

    End Sub

    ' This event handler deals with the results of the background operation.
    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object,
    ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Cancelled = True Then
            ResultLabel.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabel.Text = "Error: " & e.Error.Message
        Else

            Me.ProgressBar2.Enabled = False
            Me.ProgressBar2.Visible = False
            Me.Panel1.Enabled = True
            Me.PanelBtnBulkTag.Enabled = True
            ResultLabel.Visible = False
        End If
    End Sub

    Private Sub backgroundWorker2_RunWorkerCompleted(ByVal sender As System.Object,
    ByVal e2 As RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted


        If e2.Cancelled = True Then
            ResultLabelTag.Text = "Canceled!"

        ElseIf e2.Error IsNot Nothing Then
            ResultLabelTag.Text = "Error: " & e2.Error.Message
        Else
            ListBulk.DataSource = GetDataBulk().AsEnumerable().Select(Function(r) r.Field(Of String)("Word")).ToList()
            LblBulkCount.Text = "Words to Tag: " & ListBulk.Items.Count

            Me.ProgressBar3.Enabled = False
            Me.ProgressBar3.Visible = False
            Me.PanelBtnBulkTag.Enabled = True
            Me.Panel1.Enabled = True
            ResultLabelTag.Visible = False
            ListBulk.Refresh()
        End If
    End Sub
    Private Sub backgroundWorker3_RunWorkerCompleted(ByVal sender As System.Object,
    ByVal e3 As RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted


        If e3.Cancelled = True Then
            ResultLabelLink.Text = "Canceled!"

        ElseIf e3.Error IsNot Nothing Then
            ResultLabelLink.Text = "Error: " & e3.Error.Message
        Else

            DataGroupCallStringsBulk()

            Me.ProgressBar4.Enabled = False
            Me.ProgressBar4.Visible = False
            Me.CBList.Enabled = True

            ResultLabelLink.Visible = False
            DataGridViewStringsBulk.Refresh()

        End If
    End Sub

Open in new window

CERTIFIED EXPERT

Commented:
What was the error message?
I built a dummy app and it ran just fine.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Here's a MS page with info on how to use delegates:

https://code.msdn.microsoft.com/windowsdesktop/BackGroundWorker-25598fff
CERTIFIED EXPERT

Commented:
Also try changing it to a Sub instead of Function and use a public or private  Variable that both can access.
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you both for taking time to not only show a solution but to guide me to the use of delagates and also to IT saige for the proof of concepts I find those so so useful to understand the underlying technique.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.