Avatar of PeterBaileyUk
PeterBaileyUk

asked on 

vb.net background worker crossthread

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

Visual Basic.NET

Avatar of undefined
Last Comment
PeterBaileyUk

8/22/2022 - Mon