0

I am trying to use a timer to scan my Xbox 360 controller. But I cannot directly update my UI like the code I wrote below.

I would get a exception when I try to run this code.

An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code

Additional information: The calling thread cannot access this object because a different thread owns it.

XButton is a radiobutton on the GUI that I want to toggle.

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports System.Timers
Imports System.Windows.Threading

Public Class XboxControllerStatus
Friend WithEvents Timer1 As Timer


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Elapsed

    Dim currentState As GamePadState = GamePad.GetState(PlayerIndex.One)
    If currentState.IsConnected Then
        If currentState.Buttons.X.Pressed Then
            XButton.IsChecked = True

        Else
            XButton.IsChecked = False

        End If
    End If

End Sub

End Class

2 Answers 2

3

This works for me, all the time

Control.Invoke(sub()
                  'Put code here
               End Sub)
Sign up to request clarification or add additional context in comments.

1 Comment

This is a very good answer - not sure why it's buried with no upvotes
0

First you need to set up a delegate

Delegate Sub SetCheckBoxCallback(ByVal value As Boolean)
Friend Sub SetCheckBox(ByVal value As Boolean)
    XButton.IsChecked = value
End Sub

after that all you need to do is call the following code from within your timer to invoke it:

Dim DesiredValue as Boolean = True

Me.Dispatcher.Invoke(New SetCheckboxCallback(AddressOf SetCheckbox), New Object() {DesiredValue})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.