Hi. I have a form that is performing many calculations. Sometimes this can take maybe 10 seconds or more. In this time I wish to show user that it is busy. So I have create a progress bar (Marquee) and have put it inside panel.

So when this form is busy calculate I show progress bar. My problem is a cross-thread error. Here is my code:

Public Class frmCountHazards

    Private Sub frmCountHazards_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MyProgressBar.Visible = True
    End Sub


    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Dim con As New SqlClient.SqlConnection(My.Settings.RiskDBConnectionString)

'this is calculation. there are many more but I only put one to demonstrate. 

        Dim cmd1 As New SqlClient.SqlCommand("Select COUNT(tblRiskAssessment.Hazard) as TOTALS_Risks FROM tblContext LEFT OUTER JOIN tblRiskAssessment ON tblContext.ContextID = tblRiskAssessment.ContextID WHERE (tblContext.ContextID LIKE @ContextID) AND (tblRiskAssessment.Hazard = 'Stairs')", con)
        cmd1.Parameters.AddWithValue("ContextID", MyGlobalVariableContextID)
        Dim cnt1 As Integer = CInt(cmd1.ExecuteScalar)

this is where I get error:
 Me.TextBox1.Text = cnt1
 End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Error IsNot Nothing Then
            '' if BackgroundWorker terminated due to error

        ElseIf e.Cancelled Then
            '' otherwise if it was cancelled
            MessageBox.Show("Task cancelled!")

            'otherwise it completed normally
            MyProgressBar.Visible = False

            MsgBox("Done", vbInformation, "Done")
            Me.Enabled = True
        End If
    End Sub

End Class