473,587 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application Lock

147 New Member
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
May 6 '09 #1
6 5369
akshalika
17 New Member
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
May 6 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
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.
May 7 '09 #3
DaveRook
147 New Member
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??
May 7 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Since this code was giving during a tutor session, maybe it's being used in this scenario simply to demonstrate what Application Lock does.
May 7 '09 #5
balame2004
142 New Member
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.
May 8 '09 #6
balame2004
142 New Member
Read this for more details about usage of application lock & unlock methods.
http://msdn.microsoft.com/en-us/libr...z4(VS.71).aspx
May 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3282
by: J. Baute | last post by:
I'm caching data in the Application object to speed up certain pages on a website The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data from the "cache" hardly takes any time A basic pattern used to get the data from disk, or from cache is this data = getDataFromCache("mydata" if...
10
5110
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within this transcation. Why DB2 does not release the X lock after a failed insert??? We use DB2 UDB EEE Version 7.2 Fixpak 9, but we also can reproduce...
4
2052
by: Eidolon | last post by:
Quick question about Application.Lock()... When you lock the application, does it still allow read access, or does it deny read as well as write? thanks in advance, - Aaron.
2
1537
by: zorro | last post by:
Hello there, To lock or not to lock, that is my question... I have an arrayList in my application object. The arrayList contains objects of a class that I defined. I have my own mechanisms that ensure that only one user at a time can modify a given object's properties, but another user could modify another object's properties at the same...
5
1984
by: Homer J. Simpson | last post by:
Hi, Any idea to get the -REAL- number of active users -WITHOUT- this kind of code ? ---------------------------------------------------------------------------- --------- Sub Session_OnStart Application.Lock Application("Active")=Application("Active")+1 Application.Unlock
4
1242
by: John Cosmas | last post by:
I need to execute some threads that load items into my APPLICATION object. I haven't figured out how to do that when I fire off a thread on a page, that takes its time and loads data into the APPLICATION level object which will be used later. Here is an example code I've used to fire it off. Dim pclsUserServices As clsUserServices = New...
5
2443
by: Jennifer.Berube | last post by:
What is it exactly that makes application.lock such a bad thing? I just need a better explanation or link to a resource desribing it's downfall.
4
4513
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or...
0
1518
by: rkumar4acc | last post by:
Hi, We are encountering frequent deadlock- timout issues in QA Databases.Further looking into the lock snapshot, it seems that there is an agentID with appl hande ID as 0 and also we are not able to force this agent off. Application handle = 0 Application ID = AC15A814.P0A5.071219044747...
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.