The error "The calling thread cannot access this object because another thread belongs to it." is standard on WinForms and WPF controls.
In WPF and WinForms, windows are displayed on the screen using one specific thread, usually called a user interface thread. Each update / change action regarding the controls must take place in this thread in order to be successful.
The usual way to work with WinForms is to create a special delegate and call Control.Invoke, as shown in this link .
As in WPF, the same effect is achieved using Dispatcher . Your code should look like this:
this.Dispatcher.Invoke( () => svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 0.1));
UPDATE:
I have the following code to work in VB.NET:
Private Delegate Sub ScrollDelegate(ByVal offset As Double) Private Sub ScrollLeft(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset) End Sub // ... calling from background thread Dim slt As ScrollDelegate slt = New ScrollDelegate(AddressOf ScrollLeft) Me.Dispatcher.Invoke(slt)
Update 2
The code has changed for the question.
Dim ScrollLeft As Boolean = True Dim atimer As New System.Timers.Timer() Dim scrollMethod As ScrollDelegate Private Delegate Sub ScrollDelegate(ByVal offset As Double) // ... Me.InitializeComponent() slt = New ScrollDelegate(AddressOf DoScroll) // ... Private Sub timer_Tick(sender As Object, e As EventArgs) If ScrollLeft Then Me.Dispatcher.Invoke(slt, -1) Else Me.Dispatcher.Invoke(slt, 1) End If End Sub // ... Private Sub DoScroll(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset) End Sub
source share