troubleshooting Question

Cross-thread operation not valid

Avatar of KJLC
KJLC asked on
Visual Basic.NET
6 Comments1 Solution647 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
deadlyDev

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros