Question

I have to show many transaction on any table. It takes so much long time to process. I want to use a background process in visual basic 2010, but it always give an error message like this "Cross thread operation detected". I have try so many ways that i found in the internet, but still can't find what the problem is. Please help me about how to fix this problem. This is my code :

Private Sub CHK_A_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CHK_A.CheckedChanged
        Disabled()
        P_Panel.Visible = True
        B_Worker.RunWorkerAsync()

    End Sub

Private Sub BTN_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Search.Click

        USP_Select_Registration()
    End Sub

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

        Threading.Thread.Sleep(25)
        USP_Select_Registration()

    End Sub

    Private Sub B_Worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles B_Worker.RunWorkerCompleted
        P_Panel.Visible = False
    End Sub

This one is my store procedure

Public Sub USP_Select_Registration()
        Dim Con As New SqlConnection(SQLCon)
        Try
            Con.Open()
            Dim Cmd As New SqlCommand("USP_Select_Registration", Con)
            Cmd.CommandType = CommandType.StoredProcedure
            Cmd.Parameters.Add("@Check", SqlDbType.Bit).Value = CHK_A.EditValue
            Cmd.Parameters.Add("@Month", SqlDbType.Int).Value = CBO_Month.SelectedIndex + 1
            Cmd.Parameters.Add("@Year", SqlDbType.Int).Value = SPE_Year.EditValue
            Cmd.Parameters.Add("@Search", SqlDbType.VarChar, 150).Value = TXT_Search.Text
            DS_Registration.Tables("MST_Registration").Clear()
            Dim Adt As New SqlDataAdapter(Cmd)
            Adt.Fill(DS_Registration.Tables("MST_Registration"))
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        Finally
            Con.Close()
        End Try
    End Sub
Était-ce utile?

La solution

I think you get error because inside of USP_Select_Registration you trying to read values from form control's. Which was created by another thread.

RunWorkComleted executes in the same thread where Backgroundworker was created, thats why code P_Panel.Visible = False will executes normally.

But DoWork executes on the another thread.
And when you tried to access some forms controls to read values
TXT_Search.Text - it raise error

You can pass you searching parameters to BackgroundWorker, but you need to add parameters to USP_Select_Registration function.

For example:

Private Sub USP_Select_Registration(searchText as String)
    'Your code here
End Sub

Then where you start BackgroundWorker:

B_Worker.RunWorkerAsync(TXT_Search.Text)

And

Private Sub B_Worker_DoWork(ByVal sender As Object, 
                            ByVal e As DoWorkEventArgs) Handles B_Worker.DoWork

    Threading.Thread.Sleep(25)
    USP_Select_Registration(e.Argument)

End Sub

In your case you need to pass more then one parameter.
So you can create some structure/class or whatever object where you can keep all needed values. And pass that object to RunWorkerAsync

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top