Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,
When i am using background worker i got unclear message '
Cross-thread operation not valid: Control 'Panel4' accessed from a thread other than the thread it was created on.


but if take the routine to the normal button it is working perfectly but i got hang while the process then after finishing the process everything can be usable.

what is the correct way to use Background worker?

What I have tried:

VB
Private Sub BtnOk_Click(sender As Object, e As EventArgs) Handles BtnOk.Click
        'Try
        If BW.IsBusy = True Then
            If MsgBox("There is a process is running, Do you like to kill the process?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Process") = MsgBoxResult.Yes Then
                BW.CancelAsync()
            End If
        Else
            BW.RunWorkerAsync()
        End If

    End Sub


VB
<pre>Private Sub BW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BW.DoWork

        If Not conn.State = ConnectionState.Open Then conn.Open()
        SaveInto.Connection = conn
        SaveInto.CommandText = "DELETE FROM TrialBalance"
        SaveInto.ExecuteNonQuery()

        Select Case Period_Choice
            Case "0" 'Uptodate
                Select Case AllBr_Status
                    Case True
                        Select Case RepType
                            Case "0"
                                Select Case Level_Choice
                                    Case "1"
                                        'سلكت للمستوى الاول كاجماليات بدون تحديد اي فرع
                                        Dim Account_Code, Account_Name As String
                                        Dim Selected_row As Boolean
                                        PB.Maximum = Me.AccountsList.Rows.Count

                                        PB.Visible = True

                                        For Each r1 As DataGridViewRow In Me.AccountsList.Rows
                                            Selected_row = r1.Cells(0).Value
                                            Account_Code = Trim(r1.Cells(1).Value)
                                            Account_Name = Trim(r1.Cells(2).Value)

                                            If Selected_row = True Then
                                                SaveInto.CommandText = "INSERT INTO TrialBalance(Account,Name,OpenBalance) 
                                                            (SELECT Main_Gr_Code,Main_Gr_Name,Sum(ISNULL(OP_BAL_" & Trim(CurYear) & ",0.00))
                                                            FROM Acc_OBAL 
                                                            GROUP BY Main_Gr_Code,Main_Gr_Name
                                                           HAVING Main_Gr_Code = '" & Trim(Account_Code) & "')"
                                                If Not conn.State = ConnectionState.Open Then conn.Open()
                                                SaveInto.ExecuteNonQuery()
                                                SaveInto.CommandText = "Update TrialBalance SET TransactionBalance = 
                                                ISNULL((SELECT SUM(Dr_Acc_Value) - sum(Cr_Acc_Value) As Tot
                                                From JV_QRY
                                                Group By Main_Gr_Code, JV_Year
                                                HAVING Main_Gr_Code = '" & Trim(Account_Code) & "'
                                                And JV_Year = '" & CurYear & "'),0.00)
                                                where Account = '" & Trim(Account_Code) & "'"

                                                If Not conn.State = ConnectionState.Open Then conn.Open()
                                                SaveInto.ExecuteNonQuery()
                                            End If
                                            PB.Value += 1
                                        Next
                                        PB.Visible = False
                                        PB.Value = 0
Posted
Updated 22-Mar-17 5:48am
Comments
[no name] 22-Mar-17 11:26am    
"what is the correct way to use Background worker?", you mean you didn't even bother doing any research before posting this? https://www.codeproject.com/Questions/203749/Background-worker-problem

You can only access UI controls from the thread on which they were created - the UI thread. If you try to do anything at all with them from within a background worker - which is a different thread - you will get a cross threading exception.

Instead use the BackgroundWorker.ReportProgress method[^] to pass the information to the UI thread and let it deal with UI updates.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900