I have a DevExpress RichEditControl which is taking a very long time to load on my page, so rather than lock the UI while it loads in as static XAML, I'm using a new thread to append it to an existing DockPanel:
private bool Working { get; set; }
protected void btnAdd_Click(object sender, RoutedEventArgs e)
{
var thread = new Thread(new ThreadStart(LoadEditorView));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void LoadEditorView()
{
// Indicate that a this worker thread is busy.
Working = true;
// Create a new RichEditControl to be rendered on the Page.
var rte = new RichEditControl();
rte.CommandBarStyle = CommandBarStyle.Ribbon;
rte.DocumentSource = null;
rte.SetBinding(RichEditControl.DocumentSourceProperty, new Binding("Body"));
DockPanel.SetDock(rte, Dock.Bottom);
// Append the RichEditControl to the DockPanel that it should be in.
dpEditorPanel.Dispatcher.Invoke((() => dpEditorPanel.Children.Add(rte)));
}
When its time to add the editor to the dockpanel's children, I'm getting an InvalidOperationException saying that:
The calling thread cannot access this object because a different thread owns it.
I was to understand that using the Dispatcher was the way to go to avoid this very issue (even if it may not necessarily be the most correct way to do it in the first place).
Have I missed something? How can I fix the problem?