I have a problem setting a Property on the main thread from another thread.
This is what i do at the moment:
XAML:
<TabControl Name="tab"/>
Code Behind:
TabItem tabItem = new TabItem();
Binding myBinding = new Binding("ReportView") { Source = ViewModel, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
BindingOperations.SetBinding(tabItem, TabItem.ContentProperty, myBinding);
tab.Items.Add(tabItem);
Thread t = new Thread(DoWork);
t.SetApartmentState(ApartmentState.STA);
t.Priority = ThreadPriority.Normal;
t.Start();
DoWork Method:
When i create the ReportPreviewView inside the body of the Dispatcher.BeginInvoke, i don't get an exception:
public void DoWork(object sender)
{
//I create the relavant ViewModel
ReportViewModel reportViewModel = new ReportViewModel(SessionContext, Mediator, Parent.ReportRequest, false);
Dispatcher.BeginInvoke((Action)(() =>
{
//Create the relevant View
ReportPreviewView reportPreviewView = new ReportPreviewView(reportViewModel) { ReportName = Parent.ReportRequest.Report.ReportName };
ViewModel.ReportView = reportPreviewView;
}));
}
But this is not correct. It freezes up my UI thread. The creation of the ReportPreviewView takes quit a long time.
So then i move the creation of the ReportPreviewView outside the Dispatcher.BeginInvoke:
public void DoWork(object sender)
{
ReportViewModel reportViewModel = new ReportViewModel(SessionContext, Mediator, Parent.ReportRequest, false);
ReportPreviewView reportPreviewView = new ReportPreviewView(reportViewModel) { ReportName = Parent.ReportRequest.Report.ReportName };
Dispatcher.BeginInvoke((Action)(() =>
{
reportLoaderViewModel.ReportView = reportPreviewView;
}));
}
As soon as PropertyChanged fires i get the following exception:
The calling thread cannot access this object because a different thread owns it
Any idea how i can get around this?