Thinking with Threads in asp.net
I've got a static class that has a private dictionary object. This object is initialized in the static constructor, and is then modified every time a TimerElapsed event is raised by a timer object (also located in the static class).
My pages use a get() method to access the dictiona开发者_如何转开发ry. These are pages viewable by the public, so many of these may happen at once.
Do I need to worry about locking the dictionary object to prevent a page from trying to read it while it's being updated?
You may find that using ConcurrentDictionary (if you are using .NET 4) to be a more reliable solution. Otherwise you may run in to problems when two or more threads attempt to write (or read/write) at the same time.
If you don't have .NET 4, you may prefer to wrap up your dictionary into a class that uses locks to prevent access to the shared bits so you don't have locks scattered everywhere.
精彩评论