How to Perform Continues Iteration over Shared Dictionary in Multi-threaded Environment
Posted
by Mubashar Ahmad
on Stack Overflow
See other posts from Stack Overflow
or by Mubashar Ahmad
Published on 2010-05-20T07:55:05Z
Indexed on
2010/05/20
8:00 UTC
Read the original article
Hit count: 273
multithreading
|.NET
Dear Gurus.
Note Pls do not tell me regarding alternative to custom session, Pls answer only relative to the Pattern
Scenario
I have Done Custom Session Management in my application(WCF Service)
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.
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.
For this I added a Timer Object which calls ClearExpiredSessions function after the specific period of time. which iterates on the dictionary.
Problem:
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().
So can anybody tell me what kind of Design i should follow in this case. Is there any standard pattern available.
© Stack Overflow or respective owner