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 Object,
ByVal e As
System.EventArgs)
Handles 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
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 Object,
ByVal e As
System.EventArgs)
Handles 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