Application Lock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Application Lock

    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)?

    Code:
    public class QuoteClass : UserControl
      {
      public HtmlGenericControl divQuote;
      void Page_Load(Object source, EventArgs e)
        {
          divQuote.InnerHtml = RandomQuote();
        }
        
      public string RandomQuote()
        {
          int intRandomNum;
          string strQuote = "";
          Random rndQuote = new System.Random(GetHashCode()); 
          intRandomNum = rndQuote.Next(1,4);
          switch(intRandomNum)
            {
              case 1:  
                strQuote = "An apple a day keeps the doctor away. -- Anonymous";
                Application.Lock();
                Application["Quote1"] = Convert.ToInt32(Application["Quote1"]) + 1;
                Application.UnLock();
                break;
              case 2:
                strQuote = "Thought product and food product are to me nothing compared to the producing of them. -- Robert Frost";
                Application.Lock();
                Application["Quote2"] = Convert.ToInt32(Application["Quote2"]) + 1;
                Application.UnLock();
                break;
              case 3:
                strQuote = "Eat to live, and not live to eat. -- Benjamin Franklin";
                Application.Lock();
                Application["Quote3"] = Convert.ToInt32(Application["Quote3"]) + 1;
                Application.UnLock();
                break;
            }
            return strQuote;
        }
    	  
      }
    Thank you
  • akshalika
    New Member
    • Apr 2009
    • 17

    #2
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      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.

      Comment

      • DaveRook
        New Member
        • Jul 2007
        • 147

        #4
        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??

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Since this code was giving during a tutor session, maybe it's being used in this scenario simply to demonstrate what Application Lock does.

          Comment

          • balame2004
            New Member
            • Mar 2008
            • 142

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

            Application.Loc k locks application state, other threads waits until Application.Unl ock call occurs. Other threads can not get/set application variable when application object is being locked.

            Comment

            • balame2004
              New Member
              • Mar 2008
              • 142

              #7
              Read this for more details about usage of application lock & unlock methods.

              Comment

              Working...