troubleshooting Question background worker crossthread

Avatar of PeterBaileyUk
PeterBaileyUk asked on
Visual Basic.NET
11 Comments1 Solution401 ViewsLast Modified:
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

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")
                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
                        End If

                    End If

                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)
                    End Using

                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 & "'" & ")"
                    End Using
                Using cmdCommonInsert As New SqlCommand("usp_UpdateStrCommon", cnSql)
                    cmdCommonInsert.CommandType = CommandType.StoredProcedure
                    cmdCommonInsert.CommandTimeout = 0
                End Using
            End Using

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

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

        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.

    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


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

            ResultLabelLink.Visible = False

        End If
    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros