Connecting Tech Pros Worldwide Help | Site Map

Application Lock

Member
 
Join Date: Jul 2007
Posts: 70
#1: May 6 '09
Hi

I have some code from a friend and I don't understand why they have used the application lock feature! I understand this produces a random quote, but can some one explain to me what happens with the application lock and why it's in use (see lines 19 - 21)?

Expand|Select|Wrap|Line Numbers
  1. public class QuoteClass : UserControl
  2.   {
  3.   public HtmlGenericControl divQuote;
  4.   void Page_Load(Object source, EventArgs e)
  5.     {
  6.       divQuote.InnerHtml = RandomQuote();
  7.     }
  8.  
  9.   public string RandomQuote()
  10.     {
  11.       int intRandomNum;
  12.       string strQuote = "";
  13.       Random rndQuote = new System.Random(GetHashCode()); 
  14.       intRandomNum = rndQuote.Next(1,4);
  15.       switch(intRandomNum)
  16.         {
  17.           case 1:  
  18.             strQuote = "An apple a day keeps the doctor away. -- Anonymous";
  19.             Application.Lock();
  20.             Application["Quote1"] = Convert.ToInt32(Application["Quote1"]) + 1;
  21.             Application.UnLock();
  22.             break;
  23.           case 2:
  24.             strQuote = "Thought product and food product are to me nothing compared to the producing of them. -- Robert Frost";
  25.             Application.Lock();
  26.             Application["Quote2"] = Convert.ToInt32(Application["Quote2"]) + 1;
  27.             Application.UnLock();
  28.             break;
  29.           case 3:
  30.             strQuote = "Eat to live, and not live to eat. -- Benjamin Franklin";
  31.             Application.Lock();
  32.             Application["Quote3"] = Convert.ToInt32(Application["Quote3"]) + 1;
  33.             Application.UnLock();
  34.             break;
  35.         }
  36.         return strQuote;
  37.     }
  38.  
  39.   }
Thank you
Newbie
 
Join Date: Apr 2009
Posts: 18
#2: May 6 '09

re: Application Lock


It seems like the purpose of storing values in application state it suppose to keep track about how many times each quotes display to users. Since application state is one repository for all users it needs to handle synchronous access.

http://msdn.microsoft.com/en-us/library/ms178594.aspx
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#3: May 7 '09

re: Application Lock


Quote:
I have some code from a friend and I don't understand why they have used the application lock feature!
Sorry if this sounds snotty... But since you got the code from a friend, maybe that friend could explain why they did it that way.
Member
 
Join Date: Jul 2007
Posts: 70
#4: May 7 '09

re: Application Lock


Hi

He had a one day tutor who gave him the code but did not explain this section (it was one of the demos that they didn't have time to complete). He asked me what the application lock did (which I could semi-explain). However, I could not explain why it is being used in this instance! Why would a random quote require locking??
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#5: May 7 '09

re: Application Lock


Since this code was giving during a tutor session, maybe it's being used in this scenario simply to demonstrate what Application Lock does.
Familiar Sight
 
Join Date: Mar 2008
Posts: 141
#6: May 8 '09

re: Application Lock


Application lock and unlock used to prevent usage of application variables in other threads.

Application.Lock locks application state, other threads waits until Application.Unlock call occurs. Other threads can not get/set application variable when application object is being locked.
Familiar Sight
 
Join Date: Mar 2008
Posts: 141
#7: May 8 '09

re: Application Lock


Read this for more details about usage of application lock & unlock methods.
http://msdn.microsoft.com/en-us/libr...z4(VS.71).aspx
Reply