Cross-thread operation not valid

KJLC used Ask the Experts™
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

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
    End Sub

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    End Sub
    Public Sub CloseForm()
        aTimer.Enabled = False
    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
            End If
        Next fControl

    End Sub

End Class

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sorry if this isn't 100%... I'm a c# guy :P

Add the following delegate to the class declaration
Delegate Sub SetButs(System.Windows.Forms.Form frm)

Replace line 14 (SetButtons(theForm)) with the following:

theForm.Invoke(new SetButs(SetButtons),theForm);

that should do the trick (Invoke will execute the method on the forms main thread.)


Thanks for the help syntax slightly wrong but works

Delegate Sub SetButs(ByVal cForm As Form)

theForm.Invoke(New SetButs(AddressOf SetButtons), theForm)
Awesome! Thanks for posting the corrected syntax :D
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

What is the benefit to having this in a different class?

Since the code is going to run in the main UI thread anyways (because of the Invoke), you could switch from System.Timers.Timer to System.Windows.Forms.Timer (the one in the ToolBox).  Then you won't need an Invoke()...


I have split this off because i want to use the code for multiple forms, i didn't want to put the code in every form.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

FYI, your code will only work if the CheckBoxes are contained DIRECTLY by the Form itself.  If you re-design sometime in the future and put them in a container then it won't find them.  You could use a Recursive search instead...

Just seems like it would be faster to hold a reference to the IOItem itself in the CheckBox Tag() property instead of the name.  Then you don't need to go looking for a match.  You could also use a Shared List(Of CheckBox) and add ALL the CheckBoxes to it whenever you open a form.  This way you can iterate the list (instead of searching for the CheckBoxes), grab the IOItem directly from the Tag, and update it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial