We help IT Professionals succeed at work.
Get Started

Cross-thread operation not valid

KJLC
KJLC asked
on
644 Views
Last Modified: 2012-05-11
Hi I know this has been asked before but i am struggling.

I have a form with check boxes on.

I want to update these check boxes from another Class.

The class has a timer that loops around checking for updates.

However i get the error of Cross-thread operation not valid.

I was looking into delegates and invoking etc but cant seam to get it to work

Thanks
Public Class FormUpdateService
    Private aTimer As System.Timers.Timer
    Private theForm As Form
    Public Sub SetupForm(ByVal currentForm As form)
        aTimer = New System.Timers.Timer(10000)
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        aTimer.Interval = 1000
        aTimer.Enabled = True
        theForm = currentForm
        SetButtons(theForm)
    End Sub

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        SetButtons(theForm)
    End Sub
    Public Sub CloseForm()
        aTimer.Enabled = False
        aTimer.Dispose()
    End Sub

    Private Sub SetButtons(ByVal currentForm As Form)
        Dim localSystemSignals As SystemSignalsSingleton = SystemSignalsSingleton.GetSingleton
        Dim fControl As Control


        For Each fControl In currentForm.Controls
            If (TypeOf fControl Is CheckBox) Then
                Dim chBox As CheckBox
                Dim signals As New List(Of IOItem)
                Dim returnValue As New IOItem
                chBox = fControl
                signals = localSystemSignals.GetIOItems
                For Each signal As IOItem In signals
                    If signal.ControlName = fControl.Tag Then
                        If signal.ActualSignalValue = 1 Then
                            chBox.Checked = True
                        End If
                    End If
                Next
            End If
        Next fControl

    End Sub

End Class

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE