views:

24

answers:

4

I want to access a scrollviewer from another thread. Please tell me how to do detach it from the Main Thread so that I can change the Offsets of the scrollviewer. Thanks

A: 

WPF UIs have "thread affinity" - only the thread that creates the UI can update it.

For the above scenario, you'd have to cache the Dispatcher object (Dispatcher.CurrentDispatcher) when the UI is created. The other threads would have to delegate their code blocks to this object via an Invoke/BeginInvoke.. See this link

Gishu
A: 

Controls can only be updated from the thread that created them. Look into the BackgroundWorker class if you need to execute a time consuming operation in another thread.

Taylor Leese
A: 

You can better search SO for related questions.

Anyways the answer is here.

if (myScrollviewer.InvokeRequired) 
{ 
    myScrollviewer.BeginInvoke(new MethodInvoker(delegate { //access your myScrollviewer here } )); 
}

or you can achieve this using dispatcher

Dispatcher UIDispatcher = Dispatcher.CurrentDispatcher;  // Use this code in the UI thread

and access your myScrollviewer using the UIDispatcher object created

UIDispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>  
{  
    // access your myScrollviewer here 
}));  
Veer
Solved using Dispatcher thanks
A: 

Another way aside from using the dispatcher is to use data binding. You can bind the dependency properties like HorizontalOffset to some property of an object that you can easily access in a different thread

Michael Detras