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 void Page_Load(object
sender, System.EventArgs e)
{
Application.Lock();
int
count = 0;
if (Application("Pagecount")
!= null)
{
count =
Convert.ToInt32(Application("Pagecount"));
}
count += 1;
Application("Pagecount")
= count;
Application.UnLock();
Label1.Text =
"Total Page Visited: " + count.ToString() +
"Times";
}
No comments:
Post a Comment