Application State in ASP. NET using VB.NET

Here, we will see what is application state, advantages of application state, disadvantage of application state, Example of application state.
  • 3594
 

Here, we will see in this article.

  1. what is application state
  2. advantages of application state
  3. disadvantage of application state
  4. Example of application state

What is Application state

Application state is a global storage mechanism that is stored on the server and shared for all users. Does not expire. Thus, application state is useful for storing information that needs to be maintained between server round trips and between requests for pages. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Application state is based on the System.Web.HttpApplicationState class, which is provided in all web pages through the built-in Application object. Application object will not have any default expiration period.

Advantages of application state:

  • Application object memory released when we removed.

  • Multiuser can able to access application variable.

  • To avoid deadlock or conflict we should use Lock and Unlock when we use write or update in the application object.

  • Other application can't access this application values.

Disadvantages of application state:

  • Application variable will exists until exit our application.

  • If we do not have Lock() and Unlock, deadlock will occur.

  • Its global variable so anyone can access within this application.

Example: We can count how many times a given page has been requested by various clients. Using an application state.

Now drag and drop a label control on the form. Now double click on the form and add the following code.

Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

        Dim count As Integer = 0

        If Application("Pagecount"IsNot Nothing Then

            count = CInt(Application("Pagecount"))

        End If

        count += 1

        Application("Pagecount") = count

        Label1.Text = " total Page Visited: " & count.ToString() & "Times"

    End Sub

 

Problem with Above code

How to implement Synchronization in application state?

In the above code, a page counter would probably not keep an accurate count. For example, if two clients requested the page at the same time. Then deadlock will occur. We can avoid deadlock occurrence while we updating application variable by multiple users with help of Lock() and UnLock().

Adding the following code with lock and unlock method.

Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

        Application.Lock()

        Dim count As Integer = 0

        If Application("Pagecount"IsNot Nothing Then

            count = CInt(Application("Pagecount"))

        End If

        count += 1

        Application("Pagecount") = count

        Application.UnLock()

        Label1.Text = "Total Page Visited: " & count.ToString() & "Times"     

    End Sub

Now run the application and test it.

b0.gif
 

Figure1

Now copy the url and open new browsers and paste the same url in new browsers. The result will be as shown below:

Figure2

b1.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.