The Application Object Maintaining State

300 Timeout The session timeout, in minutes. If a new request isnt received in this amount of time, the session ends. The type is Integer. The default is 20 . Some commonly used methods of the HttpSessionState class are: Abandon Ends the current session. The syntax is: Public Sub Abandon Clear Clears all values from the Session object. The syntax is: Public Sub Clear Remove Clears a single item from the Session object. The syntax is: Public Sub RemoveByVal name As String

6.8.2 The Application Object

Some state should be maintained at the application level rather than at the session level, meaning that the state information is available to all sessions in the application. This is what the Application object is for. It is used just like the Session object, except that values stored in the Application object are visible to all sessions in the application. The Application object is instantiated when the first session begins, and it ends when no more sessions exist. Be careful when writing new values to the Application object. Because a single Application object is shared by all concurrent sessions which may be running on separate threads in IIS, threading- concurrency issues come into play. For example, consider an application in which each session stores its users name into an array that is stored in the Application object. The application might use this information to display a summary screen of all users currently active in the application. Simplistic code to accomplish this task might look like this: Wrong Dim names As ArrayList = ApplicationNames If names Is Nothing Then names = New ArrayList ApplicationNames = names End If names.AddtxtName.Text Again, this code might be executed in response to a button-click event in which the user submits some information collected on a form. It assumes that there is a text box named txtName on the form. The information thus collected can be used by any session in the application. Note that all sessions in the application add their data to the same array list. A simple page to view this information might look like this: Page AutoEventWireup=false Language=VB Debug=true script runat=server Protected Overrides Sub OnLoade As EventArgs 301 lstNames.DataSource = ApplicationNames lstNames.DataBind End Sub script html body Currently active users:br asp:ListBox id=lstNames runat=server body html Here the code is loading a ListBox control with the contents of the array list saved in the Application object. Data binding is explained in Chapt er 8 . Theres nothing conceptually wrong with this approach, but the code that saves the information needs a little more attention. The problem is that two separate requests in the same application, but in different sessions, could attempt to modify the array list at the same time. The array list is not thread- safe, so problems could ensue, possibly resulting in lost data or application exceptions. To resolve this issue, wrap the code that sets the values in calls to the Application objects Lock and UnLock methods, like this: Application.Lock Dim names As ArrayList = ApplicationNames If names Is Nothing Then names = New ArrayList ApplicationNames = names End If names.AddtxtName.Text names = Nothing Application.UnLock During the time after the Lock method has been called but before the UnLock method has been called, if code on another thread calls the Lock method, that thread will block temporarily cease to run until the code on the first thread calls the UnLock method. This ensures that two threads arent accessing the array list at the same time. The downside to this is that performance is hurt if multiple threads are blocked waiting for access to the same application data. This makes it imperative to call UnLock as soon as possible and not to forget to call it. The Application object is an instance of the HttpApplicationState class defined in the System.Web namespace. Commonly used properties of the HttpApplicationState class are: AllKeys An array of all keys used in the Application object. A key is the name of the data being stored in the Application object, as opposed to the value of that data. For example, in the following line of code, the session key is Names : ApplicationNames = names The following code fragment loops through all the keys in the Application object: For Each str In Application.AllKeys ... Next The type of the AllKeys property is String Array. 302 Count The number of items stored in the Application object. The type is Integer. StaticObjects A collection of objects declared with the object scope=Application tag in the applications global.asax file. The type is HttpStaticObjectsCollection defined in the System.Web namespace. See Sect ion 6.9 later in this chapter. The commonly used methods of the HttpApplicationState class are: Clear Clears all values from the Application object. The syntax is: Public Sub Clear Lock As explained earlier, prohibits code in other sessions from writing to the Application object until UnLock is called. The syntax is: Public Sub Lock Remove Clears a single item from the Application object. The syntax is: Public Sub RemoveByVal name As String UnLock Unlocks the Application object after a call to Lock. The syntax is: Public Sub UnLock

6.8.3 Cookies