Welcome to my blog, stay tunned :
Home | Blogs | Stephane Eyskens's blog

SharePoint 2010 Client Object Model, synchroneous and asynchroneous behaviors

Hi,

I've seen and I got a lot of questions regarding the client object model and its two execution methods which are:

- ExecuteQuery() => synchroneous
- ExecuteQueryAsync(callback success,callback failure) => asynchroneous

As a quick reminder, if you execute the following code:

The code waits until SharePoint sends a response back. This can be useful if you want to make your code conditional upon SharePoint's answer.

While, here:

The code keeps going and does not wait for SharePoint's response. So far, so good, probably everybody is well aware of the difference between a synchroneous and an asynchroneous operation.

Where this can become confusing is that the client OM for Silverlight exposes both methods, so we might think that we can use ExecuteQuery() from within a Silverlight application and actually, you can but the result will be the same than using ExecuteQueryAsync().

The reason for that is that Silverlight does not allow you to block the main UI thread, so if you run this piece of code:

from Main.Xaml.Cs, you'll get the following exception:

and if you follow the instructions of the error message stating that you should use a seperate thread, it will indeed work but you'll get again an asynchroenous behavior. So, the conclusion is for those who are still confused:

Silverlight prevents synchroneous methods to avoid blocking the UI process, meaning that by design, it is not possible. So, the ExecuteQuery() method is just there to allow you to implement your own asynchroneous framework if needed.

Happy coding!