title: Synchronizing Exchange WebService Managed API method calls
body: <p>I have written a listener class which creates an HTTP listener and subscribes its URL to push subscriptions (on new mail event on Inbox using EWS Managed API), receives notifications, processes mails and then move those mails to Deleted Items folder. I have also written a poller which periodically checks for any mails left out inside the inbox, processes them and then moves them to the Deleted Items folder. Listener and poller run in different threads.</p>
  
  <p>I have synchronized these threads using locks. And the lock functionality is tested to work fine. However due to locking I am getting some undesired behavior.</p>
  
  <p>Consider listener receives a push notification. It starts processing it. Meantime, poller starts and it acquires the lock. Now when listener tries to acquire the lock it finds that the lock is already acquired by the poller. So it waits till lock gets freed. Say listener executes till instruction <code>INSTR-1</code> and continue waiting. Now poller is processing all mails in inbox for say 3-5 minutes. After that, poller releases the lock. In those 3-5 minutes, exchange continues to dispatch push notifications, however since first listener thread is stuck at <code>INSTR-1</code> no more listener threads are dispatched by .NET runtime. But as soon as poller releases lock, all threads gets dispatched. But since their corresponding mails have been already processed and moved to Deleted Items folder by the poller, those listener threads throws the exception “The specified object was not found in the store.”  on <code>EmailMessage.Bind()</code> method. Since I have now well analyzed this behavior and I know exactly why this exception is occuring, I can simply go on to suppress these exceptions (say with empty <code>catch</code> blocks).</p>
  
  <p>However I will like to know, </p>
  
  <ol>
  <li><p>if there is any way by which I can make Exchange Server to not to dispatch any push notifications after poller starts or in other words for those mails which have been already retrieved by EWS Managed API call <code>ExchangeService.FindItems()</code> made by poller. </p></li>
  <li><p>if there is any standard / Microsoft recommended way  to handle such problems</p></li>
  </ol>
  
  <p>We can visualize the listener and poller procedures and execution behavior as follows:
  <img src="https://social.technet.microsoft.com/Forums/getfile/427495" alt="enter image description here"></p>