1. application objects can be used to implement applications Program Status Management
(1) ApplicationApplication:
◆ Count the current number of people on the site
◆ It can be used for communication between different users on the website
◆ You can record the number of clicks on an advertisement bar
◆ Dynamic information can be displayed on each Homepage
◆ Data can be extracted from the database for multiple webpages
(2) createApplicationVariable syntax
Application ("Variable name") Example: Application ("welcome") ="Welcome to my homepage!"
(3) Application Variables will not die by themselves. Note the following when using them:
● Application variables always occupy the memory. Creating too many variables reduces the response speed.
● application the variable is aborted in three cases: the service is suspended. global. asa changed or application uninstalled
(4) useApplicationObject syntax
Application.Attribute │ Method
(5) ApplicationObject Method
Application Is used to process Application The problem of Data Writing in. There are two in total: LockMethod: prevent other clients from changing Application Object value. ( Only the current customer can modify and access ); unlock method: and lock opposite method, allow other clients to change application object value.
usage:
Application. Lock-LockApplicationAll object variables
Application. Unlock-UnlockApplicationAll object variables
For example;
Application. lock;
Application ("numcount") = Application ("numcount") + 1;(Numcount is the variable set in the application_onstart event)
Application. Unlock;
(6)ApplicationObject event
ApplicationThere are two events:
Application_onendApplication_onstart
Application_onstart:When eachApplicationThe event is activated when the object starts.
Application_onend: When eachApplication The event is activated when the object ends.
The preceding two events must be placed in Global. asax file
(7) application set of objects
application there are two sets:
1 , contents data set
contents allow the program to obtain all the application Object
Note: not included<Object>Mark andCreateobjectObject created by the method
Syntax structure:
application. CONTENTS (application Object Name )
2. staticobjects data set can be obtained with indicates the created application Object
Application. staticobjects (Object variable name)
Exploitation<Object>Syntax for marking object creation:
<Object runat = server scope =ObjectId =Instance Object Name>
Important method calls of the Application object:
· Add method: add an object to the stat set of the Application Object
For example:
Application. Add ("string1", "test ")
It indicates adding a string named string1 to the stat set of the application, whose value is "test". In fact, its effect and
Application ("string1") = "test"
And application. Item ("string1") = "test" are the same.
· Remove method, which is used to delete data from the stat set of the Application Object Based on the given identifier
For example:
Application. Remove ("string1 ")
The shared object string1 identified as string1 is deleted from the stat set of the Application object. You can use it to clear the objects added using the add method.
· Removeall method to clear all objects in the stat set of the Application object. In our examples of using properties, we have seen its usage, but it is worth noting that, we do not advocate using it because it is unclear in the programming process whether other pages depend on the public variables of an application. Once cleared, unpredictable errors will occur.
· The clear method works the same way as the removeall method.
· The get method allows the use of name identifiers or subscripts to obtain the object elements in the stat set of the Application object.
For example:
Dim TMP as object
TMP = application. Get ("string1 ")
Or TMP = application. Get (0)
Indicates getting objects marked as string1 or 0 from the stat set of the Application Object
It is equivalent:
TMP = Application ("string1 ")
TMP = Application (0)
Or
TMP = application. Item ("string1 ")
TMP = application. Item (0)
· Set Method: Modify the value of the object corresponding to the specified identifier in the Application Object stat set.
For example:
Application. Set ("string1", "try ")
Change the value "test" we set for the string1 variable to "try", which is the same as the following:
Application ("string1") = Try
· The getkey method obtains the Identification name of the corresponding object in the stat set of the Application Object Based on the given subscript.
For example:
Dim namestr as string
Namestr = application. getkey (0)
Obtains the Identification name of the first object in the stat set of the Application object.
· Lock method to lock the access permissions of other threads to the stat set in the Application object. This method is mainly used to prevent the impact of other concurrent programs on the variable operation of the application. For example, if you do not lock the log, dirty read and write may occur. For example, start to get the record value 1 from the variable,
If a log is written back to the variable, and a log is written back to the variable on the other page, the value in the variable is 2, not the actual 3. If the locking mechanism is used to read the variable to the count and write it back to the variable on the page, even if another count occurs, because the variable is locked, it is also impossible to succeed before the variable is written back. only wait for the variable to be released to form the serialization of the two variable operations, avoiding dirty Data Reading and dirty writing.
· The unlock method locks the stat set of the Application Object and releases resources for other pages.
The following is an example of the method we have learned. To emphasize the lock method and the unlock method, we will give a separate example. In this example, we create six Application variables and assign them with numerical numbers. There are three buttons on the page: Add 1, subtract 1, and clear. When you click the "Add 1" button, we will see that the value of the variable increases by 1. When you click the "minus 1" button, the variable value is reduced by 1. When you press the Clear button, all the variables disappear. In the clear function, to demonstrate the remove and clear methods at the same time, clear the last three methods with clear, and remove the other methods one by one.
1. source program
<! -- File name: Application \ formapplication01.aspx -->
<HTML>
<Script language = "VB" runat = Server>
Sub page_load (O as object, e as eventargs)
Dim I as integer
If not ispostback
For I = 1 to 6
Application. Add ("item" & I, I)
Next
End if
Response. Clear
For I = 0 to application. Count-1
Response. Write (application. getkey (I) & "=" & application. Get (I) & "<br> ")
Next
End sub
Sub addone (S as object, e as eventargs)
'Variable value plus 1
Dim I as integer
Dim J as integer
Dim t as string
For I = 0 to application. Count-1
J = application. Get (I) + 1
T = application. getkey (I)
Application. Set (T, J)
Next
Page_load (S, E)
'Refresh the screen
End sub
Sub subone (S as object, e as eventargs)
'Variable minus 1
Dim I as integer
Dim J as integer
Dim t as string
For I = 0 to application. Count-1
J = application. Get (I)-1
T = application. getkey (I)
Application. Set (T, J)
Next
Page_load (S, E)
'Refresh the screen
End sub
Sub gone (S as object, e as eventargs)
'Clear all variables
Dim I as integer
For I = 0 to application. Count-3
Application. Remove (I)
Next
'Demonstrate the Remove Method
Application. Clear
'Demonstrate the clear method
Page_load (S, E)
'Refresh the screen
End sub
</SCRIPT>
<Head>
<Title>
Application Method Test
</Title>
</Head>
<Body bgcolor = # ccccff>
<Center>
<H2> application method test </H2>
<HR>
<Form runat = Server>
<Asp: button type = "Submit" text = "+ 1" onclick = "addone" runat = server/>
<Asp: button type = "Submit" text = "-1" onclick = "subone" runat = server/>
<Asp: button type = "Submit" text = "clear" onclick = "gone" runat = server/>
</Form>
</Center>
</Body>
</Html>
2. output screen at the beginning:
3. When you click + 1 twice, the output variable value is:
4. When "-1" is clicked, the variable value is:
5. After you click "clear", the output image is displayed.
Next, let's look at an example of using the lock and Unlock methods to create a counter: The application object is shared with different connectors, so it is suitable for creating a counter.
ProgramCodeOmitted:
--------------------------
Asp.net improves Application Object Storage speed
In traditional ASP, we use application objects to store the variables applied to the entire application. Of course, this will bring about the cost of memory consumption. In. net, we can use static variables to improve it. Using static variables, the storage speed is faster than the Application Object in most cases.
Practice:
Create a webapplication named webapplication1 and add a static member sgreeting to the global class of Global. aspx.
Public class Global: system. Web. httpapplication
{
Public static string sgreeting = "China is great! ";
......
}
Add a label named label1.
Assign a value to the text attribute of label1 in page_load.
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = webapplication1.global. sgreeting;
}
After running the program, you will see China is great!