tag with the scope attribute set to Application.
Note that static objects and actual state values are stored in separate collections. The exact type of the static collection is HttpStaticObjectsCollection.
Methods of the HttpApplicationState Class
The set of methods that the HttpApplicationState class features are mostly specialized versions of the typical methods of a name/value collection. As Table 17-3 shows, the most significant extension includes the locking mechanism necessary to serialize access to the state values.
Table 17-3. HttpApplicationState Methods
Method
Description
Add
Adds a new value to the collection. The value is boxed as an object.
Clear
Removes all objects from the collection.
Get
Returns the value of an item in the collection. The item can be specified either by key or index.
GetEnumerator
Returns an enumerator object to iterate through the collection.
GetKey
Gets the string key of the item stored at the specified position.
Lock
Locks writing access to the whole collection. No concurrent caller can write to the collection object until UnLock is called.
Remove
Removes the item whose key matches the specified string.
RemoveAll
Calls Clear.
RemoveAt
Removes the item at the specified position.
Set
Assigns the specified value to the item with the specified key. The method is thread-safe, and the access to the item is blocked until the writing is +completed.
UnLock
Unlocks writing access to the collection.
Note that the GetEnumerator method is inherited from the base collection class and, as such, is oblivious to the locking mechanism of the class. If you enumerate the collection using this method, each returned value is obtained through a simple call to one of the get methods on the base NameObjectCollectionBase class. Unfortunately, that method is not aware of the locking mechanism needed on the derived HttpApplicationState class because of the concurrent access to the application state. As a result, your enumeration is thread-safe. A better way to enumerate the content of the collection is by using a while statement and the Get method to access an item. Alternatively, you can lock the collection before you enumerate.
State Synchronization
Note that all operations on HttpApplicationState require some sort of synchronization to ensure that multiple threads running within an application safely access values without incurring deadlocks and access violations. The writing methods, such as Set and Remove, as well as the set accessor of the Item property implicitly apply a writing lock before proceeding. The Lock method ensures that only the current thread can modify the application state. The Lock method is provided to apply the same writing lock around portions of code that need to be protected from other threads’ access.
You don’t need to wrap a single call to Set, Clear, or Remove with a lock/unlock pair of statements—those methods, in fact, are already thread-safe. Using Lock in these cases will only have the effect of producing additional overhead, increasing the internal level of recursion.
// This operation is thread-safe
Application["MyValue"] = 1;
Use Lock instead if you want to shield a group of instructions from concurrent writings:
// These operations execute atomically
Application.Lock();
int val = (int) Application["MyValue"];
if (val < 10)
Application["MyValue"] = val + 1;
Application.UnLock();
Reading methods such as Get, the get accessor of Item, and even Count have an internal synchronization mechanism that, when used along with Lock, will protect them against concurrent and cross-thread readings and writings:
// The reading is protected from concurrent read/writes
Application.Lock();