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:
Private Sub BtnOk_Click(sender As Object, e As EventArgs) Handles BtnOk.Click
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
<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"
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