Home » MS OfficeRSS

Cross-thread operation not valid: Control 'lblStatus' accessed from a thread other than the thread i

Hello I'm getting the 'System.InvalidOperationException' error on acccessing a thread from other than where it was created. My code is:

 'Required by the Windows Form Designer
    Public components As System.ComponentModel.IContainer ' changed from private

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    Friend WithEvents pBar1 As System.Windows.Forms.ProgressBar
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.pBar1 = New System.Windows.Forms.ProgressBar
        Me.lblStatus = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'pBar1
        '
        Me.pBar1.Location = New System.Drawing.Point(72, 128)
        Me.pBar1.Maximum = 100
        Me.pBar1.Name = "pBar1"
        Me.pBar1.Size = New System.Drawing.Size(384, 23)
        Me.pBar1.TabIndex = 0
        '
        'lblStatus
        '
        Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblStatus.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblStatus.Location = New System.Drawing.Point(24, 32)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(472, 56)
        Me.lblStatus.TabIndex = 1
        Me.lblStatus.Text = "Label1"
        Me.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'frmMain_Export
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(528, 266)
        Me.Controls.Add(Me.lblStatus)
        Me.Controls.Add(Me.pBar1)
        Me.Name = "frmMain_Export"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "MaxTrax File Export"
        Me.ResumeLayout(False)

    End Sub

#End Region

 

Private Sub OnUploadStatusChanged(ByVal sender As System.Object, ByVal e As UploadEventArgs)
        pBar1.Value = e.ProgressValue
        lblStatus.Text = e.ProgressText  'getting cross0thread operation error here.

End Sub

Public Class UploadEventArgs
    Inherits EventArgs
    Private _progressValue As Integer
    Private _progressText As String

    Public Sub New()
        _progressValue = 0
        _progressText = String.Empty
    End Sub

    Public Sub New(ByVal progressValue As Integer, ByVal progressText As String)
        _progressValue = progressValue
        _progressText = progressText
    End Sub

    Public Property ProgressValue() As Integer
        Get
            Return _progressValue
        End Get
        Set(ByVal Value As Integer)
            _progressValue = Value
        End Set
    End Property

    Public Property ProgressText() As String
        Get
            Return _progressText
        End Get
        Set(ByVal Value As String)
            _progressText = Value.Trim
        End Set
    End Property
End Class

------

' Where the lblStatus.Text = e.ProgressText (from the code below).

Public Sub StartExport()
        Dim ProgessText As String
        Dim Index As Integer
        Dim Error_Code As Integer
        Dim LogPath As String = ConfigurationManager.AppSettings("logPath")

        Index = 50

        ProgessText = "DELETING RECORDS FROM LOG TABLES"
        RaiseEvent StatusChanged(Me, New UploadEventArgs(Index, ProgessText))
        Index += _seed
        Thread.Sleep(_delayTime)
        PurgeTables()

        If Error_Code = 0 Then
            ProgessText = "EXPORTING COMPLETED STUDENT LESSON INFO"
            RaiseEvent StatusChanged(Me, New UploadEventArgs(Index, ProgessText))
            Index += _seed
            Thread.Sleep(_delayTime)
            ExportFile()
        End If

-----------------

Any suggestions on how to fix the cross thread is appreciated. Using Visual Studio 2008. (VB.NET).

Thanks in advance:-)

SP.

 

3 Answers Found

 

Answer 1

You will need to set up a Delegate for both the progressbar  and label. Here is an example for the progress bar

    Private Delegate Sub delSetProgress(ByVal newValue As Integer)

    Private Sub SetProgress(ByVal newValue As Integer)
        If pBar1.InvokeRequired Then
            Dim MyDelegate As New delSetProgress(AddressOf SetProgress)

            pBar1.Invoke(MyDelegate, New Object() {newValue})
        Else
            pBar1.Value = newValue
        End If

    End Sub

    Private Sub OnUploadStatusChanged(ByVal sender As System.Object, ByVal e As UploadEventArgs)
        SetProgress(e.ProgressValue)
    End Sub

 

Answer 2

Thanks so much for the code. It works great:-)

SP.

 

 

Answer 3

Sweet example thx.

I was able to accomplish this with a gridview and datatable waiting for the returning resultset.

  Sub bindOrders()
        tquery = New Query()
        tquery.openConnection()
        Dim o As New Orders(tquery, tappname)
        showProgressBar(True)
        setDgvPossibleDuplicates(o.getDuplicateOrders(Me.dtpDuplicates.Value))
        showProgressBar(False)
    End Sub

   Private Sub setDgvPossibleDuplicates(ByVal newvalue As DataTable)
        If dgvPossibleDuplicates.InvokeRequired Then
            Dim myDelegate As New delSetDgvPossibleDuplicates(AddressOf setDgvPossibleDuplicates)
            dgvPossibleDuplicates.Invoke(myDelegate , New Object() {newvalue})
        Else
            dgvPossibleDuplicates.DataSource = newvalue
            dgvPossibleDuplicates.Refresh()
        End If
    End Sub

    Private Delegate Sub delSetDgvPossibleDuplicates(ByVal newVal As DataTable)

Private Delegate Sub delSetProgress(ByVal newValue As Boolean)

    Private Sub showProgressBar(ByVal newValue As Boolean)
        If PictureBox1.InvokeRequired Then
            Dim MyDelegate As New delSetProgress(AddressOf showProgressBar)
            PictureBox1.Invoke(MyDelegate, New Object() {newValue})
        Else
            PictureBox1.Visible = newValue
        End If
    End Sub

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter