Avatar of KJLC
KJLC

asked on 

Cross-thread operation not valid

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

Visual Basic.NET

Avatar of undefined
Last Comment
Mike Tomlinson
ASKER CERTIFIED SOLUTION
Avatar of deadlyDev
deadlyDev
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of KJLC
KJLC

ASKER

Thanks for the help syntax slightly wrong but works

Used
Delegate Sub SetButs(ByVal cForm As Form)

theForm.Invoke(New SetButs(AddressOf SetButtons), theForm)
Avatar of deadlyDev
deadlyDev
Flag of Spain image

Awesome! Thanks for posting the corrected syntax :D
Avatar of Mike Tomlinson
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()...
Avatar of KJLC
KJLC

ASKER

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.
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.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo