When adding a new event receiver to a SharePoint 2010 entity with Visual Studio 2010 you get variety of events – some of which are synchronous and some asynchronous.
What’s the difference and when to use which?
Synchronous events run the event in the same thread before sending the Web response back to the browser. Asynchronous events are run by a separate thread, so processing does not block the flow of code execution.
For example, an ItemAdding event is always synchronous – because in your code you may want to change the course of actions (you can cancel the event or change the item properties). In such case the thread will wait to see what you have decided to do.
In other case ItemAdded, can be asynchronous or synchronous and when adding a new receiver to the solution, in the Elements.xml of your receiver definition you can specify the value of Synchronization:
.... <Class>SharePointProject1.EventReceiver1.EventReceiver1</Class> <SequenceNumber>10000</SequenceNumber> <Synchronization>Synchronous</Synchronization> </Receiver>
So what does this all mean? If you create two event receivers for the same event for the same list (or anything else for that matter) – the synchronous event will take precedence. If both events are synchronous – the order will depend on the sequence specified in the definition Elements.xml.
You also might want to know that you can not make the event, such ItemAdding, which is synchronous by default to be asynchronous, otherwise you will get an exception during the deployment with the following error:
Error occurred in deployment step 'Activate Features': Operation is not valid due to the current state of the object.
Good Luck!
