I have a windows application which consists of Analytics Tests. When user selects the particular tests then these tests run in background thread. In background Input Files (in excel, text) gets loaded in SQL and corresponding queries get executed and output get pasted in excel.
Now I have implemented some validations regarding whether input files are correct or not etc..in background thread.
So earlier it was showing all the tests selected by user in Table Layout Panel using CreateWait() function in UI thread. But now after validation I need to show only validated tests in Table Layout Panel. Now I want to update the Panel in background thread. But it is giving me error "Cross-threaded operation is invalid" saying Panel is accessed from thread different from the thread it is created on.
Below is my code: ( I am just showing only specific code/functions to in order to reader to understand the flow)
Private Sub Btn_run_Click(sender As Object, e As EventArgs) Handles btn_run.Click
CreateWait()
BackgroundWorker1.RunWorkerAsync()
End Sub
Below is the CreateWait() method which I call from UI thread and I want to call it gain from background thread.
Public Sub CreateWait()
Dim ratio1 As Double = 0
Dim ratio2 As Double = 0
If currstate = 2 Then
ratio1 = maxwidth / panelwidth
ratio2 = maxheight / panelheight
End If
Dim TestArray As Array
Dim EachTest As Array
'ExecutionWait.TestsIdName = TestsIdName
TestArray = Split(TestsIdName, ";")
Dim Pnl2 As New Panel With {
.Location = New System.Drawing.Point(30, 80),
.Anchor = AnchorStyles.None,
.Dock = DockStyle.None,
.AutoScroll = True,
.Size = New Size(946, 345),
.Margin = New Padding(0, 0, 0, 0),
.Padding = New Padding(0, 0, 0, 0),
.BackColor = Color.Transparent
}
Pnl2.HorizontalScroll.Visible = False
Pnl2.HorizontalScroll.Enabled = False
Pnl2.BorderStyle = BorderStyle.None
Pnl2.Name = "panel_output"
ExecutionWaitPanel.Controls.Add(Pnl2)
If currstate = 2 Then
Pnl2.Location = New Point(Pnl2.Left * ratio1, Pnl2.Top * ratio2)
Pnl2.Size = New Size(Pnl2.Width * ratio1, Pnl2.Height * ratio2)
End If
Dim tlp2 As New TableLayoutPanel With {
.Location = New System.Drawing.Point(0, 0),
.Dock = DockStyle.None,
.AutoScroll = False,
.AutoSize = True,
.Margin = New Padding(0, 0, 0, 0),
.Padding = New Padding(0, 0, 0, 0),
.BackColor = Color.Transparent,
.ColumnCount = 3,
.RowCount = UBound(TestArray)
}
Below is the code for Background_worker in which I call ColumnMapping function for validations and want to update ExecutionWaitPanel by calling CreateWait() function but giving me cross-threaded operation is invalid.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Call ColumnMapping(qwe, dt1.Rows(0)("input_path"))
CreateWait()
End Sub