Thread communcation problem.

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
Hi, i am using a timer

VB.NET:
        aTimer= New System.Timers.Timer(5000)
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        aTimer.Enabled = True

That resets my forms settings. But because it is on a different thread i get cross thread exception.

How can i resolve this?

VB.NET:
myButtons(True)

VB.NET:
    Private Sub myButtons(ByVal d As Boolean)
            For Each c As Control In me.Controls
                CType(c, Button).Enabled = d
            Next c
    End Sub
 
I suggest you have a look into the invoke command.
For example here's a few lines from the project I'm working on right now.

VB.NET:
Private Delegate Sub UpdateListDelegate()

VB.NET:
        Dim UpdateListDel As UpdateListDelegate
        UpdateListDel = AddressOf UpdateComputerList
        Invoke(UpdateListDel) ' Updates the list cross thread
 
Why don't you use the Forms.Timer since it's for forms usage? Just drop one to form from Toolbox.
In case you for unknown reason must use the Timers.Timer set the SyncronizingObject property to have it raise the event on UI thread.
 
Back
Top