title: How to Perform Continues Iteration over Shared Dictionary in Multi-threaded Environment
body: <p><strong>Note</strong>
  <em>Pls do not tell me regarding alternative to custom session, Pls answer only relative to the Pattern</em></p>
  
  <p><strong>Scenario</strong></p>
  
  <p>I have Done Custom Session Management in my application(WCF Service)</p>
  
  <p>for this I have a Dictionary shared to all thread. When a specific function Gets called I add a New Session and Issue SessionId to the client so it can use that sessionId for rest of his calls until it calls another specific function, which terminates this session and removes the session from the Dictionary. </p>
  
  <p>Due to any reason Client may not call session terminator function so i have to implement time expiration logic so that i can remove all such sessions from the memory. </p>
  
  <p>For this I added a Timer Object which calls ClearExpiredSessions function after the specific period of time. which iterates on the dictionary.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>As this dictionary gets modified every time new client comes and leaves so i can't lock the whole dictionary while iterating over it. And if i do not lock the dictionary while iteration, if dictionary gets modified from other thread while iterating, Enumerator will throw exception on MoveNext(). </p>
  
  <p>So can anybody tell me what kind of Design i should follow in this case. Is there any standard pattern available.</p>