kamagra how much to take

Fix Unhandled Error in Silverlight 2 Application Invalid cross-thread access error

Had a support call from a client today regarding a problem them were running into with Client OM access from a Silverlight application. The exact error was “Invalid cross-thread access”. Sitting down with the developer at the site I determined that this problem can be caused by two issues:

1) Your XAP is not copied into the right location. It is supposed to go into the 14 hive ClientBin directory under LAYOUTS. If the XAP is not present, this error can occur.

2) Objects in code and not being loaded correctly (this turned out to be the primary issue for my particular problem). Consider the following simply ListItem update code:

  1. private void UpdateData()
  2. {
  3. ClientContext clientContext = new ClientContext("url");
  4. Web web = clientContext.Web;
  5. ListCollection listCollection = web.Lists;
  6. list = listCollection.GetByTitle("TestList");
  7. ListItem li = list.GetItemById(1);
  8. li["Title"] = "My new item";
  9. li.Update();
  10. clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
  11. }
  12.  
  13. private void onQuerySucceeded (object sender, ClientRequestSucceededEventArgs args)
  14. {
  15. // I am not displaying show ShowMessage, but assume that it writes the list title to standard output
  16. UpdateUIMethod updateUI = ShowMessage;
  17. this.Dispatcher.BeginInvoke(updateUI);
  18. }
  19.  
  20. private void onQueryFailed (object sender, ClientRequestFailedEventArgs args)
  21. {
  22.  
  23. }

Now, looking back at the error, what’s missing in the above code is the load! The objects will not be initialized with this code, which can cause that particular error.
To fix it, include the load, such as (fully qualified instantiation demoed, you would obviously use your own object conventions):

  1. ClientContext.Load(List,
  2.                 list => list.Title);

before executing the query, and the error should go away!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>