As per our requirement we have to open a WPF window in a new UI thread.
We are opening the window in a new UI thread from the main UI Thread using following code:
Thread winthread = new Thread(new ThreadStart(() =>
{
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(
Dispatcher.CurrentDispatcher));
Window windowObj = new Window();
Grid gridObj = new Grid();
MyUserControl ctrl = new MyUserControl();
gridObj.Children.Add(ctrl);
windowObj.Content = gridObj;
windowObj.Show();
System.Windows.Threading.Dispatcher.Run();
}));
winthread.IsBackground = true;
winthread.SetApartmentState(ApartmentState.STA);
winthread.Start();
The window will show with MyUserControl as content when the above code executes.
I am doing some animation like flipping my usercontrol on the mouse double click event.
When I double clicked on it the application starts throwing following exception:
The calling thread cannot access this object because a different thread owns it.
on line System.Windows.Threading.Dispatcher.Run().
Can anyone suggest the solution for this problem?
MyUserControlwith just aButton). Can you show the code forMyUserControl? Can you show how you are calling this code? From an existing WPF application? Console application? - J...