
278
|
Chapter 13: The HttpApplicationState Class
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
UnLock. Win32 programmers may recognize the similarity between the use of
Lock and Unlock in the
HttpApplicationState class and Win32 critical sections.
The example above could be rewritten to be thread safe as follows:
Application.Lock( )
LocalVal = Application("Counter")
Application("Counter") = LocalVal + 1
Application.UnLock( )
While using Lock and UnLock solves the problem, their use should be absolutely
essential. For instance, synchronization of Application-level variables is not
required in the Application start or Application end event, since these events are
called only once—just before the first client operation starts and just after the last
client operation ends, respectively. Excessive use of Lock and UnLock can
degrade both performance and scalability.
In addition to properties and methods provided for backwards compatibility with
classic ASP, the ASP.NET version of the Application object adds several useful
new properties and methods, including the AllKeys collection, the Count prop-
erty, and the Clear, Get, GetKey, RemoveAll, RemoveAt, and Set methods.
In this chapter, we’ll use the following code listing as the basis for most examples
in the chapter. Unless otherwise noted, each example consists of just the Page_
Load event ...