Error: 'Invalid cross-thread access' while using the Silverlight Client Object Model
-
Tuesday, July 27, 2010 8:51 AM
Hi,
I'm trying to fill a datagrid with data from a list but I keep getting error: 'Invalid cross-thread access'.
Steps:
- Get ID from a item selected in a ComboBox from list (Solution Frameworks)
- Use the ID field above to find items with this ID in another list (Solution Framework_Mapping)
- Bind the returned listitems from Step 2 to a datagrid
Here's what my code looks like:
private void OfferingComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { object strSolutionFrameworkSelected; ListItem offerings = (ListItem)OfferingComboBox.SelectedItem; strSolutionFrameworkSelected = offerings["Offering_x0020_Name"]; var context = new ClientContext(ApplicationContext.Current.Url); context.Load(context.Web); List lst = context.Web.Lists.GetByTitle("Solution Frameworks"); context.Load(lst); var query = new Microsoft.SharePoint.Client.CamlQuery(); var strQuery = "<View><Query><Where><Eq><FieldRef Name='OID_x003a_Offering_x0020_Name' /><Value Type='Lookup'>" + strSolutionFrameworkSelected + "</Value></Eq></Where></Query></View>"; query.ViewXml = strQuery; _projectItems = lst.GetItems(query); context.Load(_projectItems); context.ExecuteQueryAsync(OnSucceededListenerGetOfferingDetails, null); } private void OnSucceededListenerGetOfferingDetails(object sender, ClientRequestEventArgs args) { sfID = _projectItems[0].Id; BindData(); } private void BindData() { var context = new ClientContext(ApplicationContext.Current.Url); context.Load(context.Web); List lst = context.Web.Lists.GetByTitle("Solution Framework_Mapping"); context.Load(lst); var query = new Microsoft.SharePoint.Client.CamlQuery(); var strQuery = "<View><Query><Where><Eq><FieldRef Name='SFID' /><Value Type='Lookup'>" + sfID + "</Value></Eq></Where></Query></View>"; query.ViewXml = strQuery; _projectItems = lst.GetItems(query); context.Load(_projectItems); context.ExecuteQueryAsync(OnSucceededListenerGetOfferings2, null); } private void OnSucceededListenerGetOfferings2(object sender, ClientRequestEventArgs args) { var list = new List<Project>(); foreach (var li in _projectItems) { list.Add(new Project { OID = li["ID"].ToString() }); } dataGrid1.ItemsSource = list; }
After the code steps over:
context.ExecuteQueryAsync(OnSucceededListenerGetOfferingDetails, null);
it then jumps to:
dataGrid1.ItemsSource = list;
throwing the error. Could point me to a reference or example where I can learn about the cross-thread access error?
Thank you!
All Replies
-
Tuesday, July 27, 2010 11:19 AM
You need to use the Dispatcher to get to the UI thread.
Try this: ( note I did not run this so you might need to play with the syntax)
private void OnSucceededListenerGetOfferings2(object sender, ClientRequestEventArgs args)
{Deployment.Current.Dispatcher.BeginInvoke(UpdateList);
}
private void UpdateList()
{
private void UpdateList
var list = new List<Project>();
foreach (var li in _projectItems)
{
list.Add(new Project
{
OID = li["ID"].ToString()
});
}
dataGrid1.ItemsSource = list;
}
Darrin Bishop Darrin Bishop Group, Inc.- Marked As Answer by Condor10101010101 Tuesday, July 27, 2010 4:09 PM
-
Tuesday, July 27, 2010 4:10 PM
Darrin,
That was right on! I will have to do some reading about the 'Dispatcher'.
Appreciate your help!
-Andres

