ASP Web Application Quick Start for. NET (2)

Source: Internet
Author: User

ASP Web Application Quick Start for. NET (2)
Application lifetime


An ASP. NET application is created when the server is requested for the first time. No ASP. NET code is executed before that. After accepting the first request, an HttpApplication instance pool is created and the Application_OnStart event is activated. The HttpApplication instance processes the request and subsequent requests until the final instance exits and then triggers the Application_OnEnd event.

Note that the Init and Dispose methods of HttpApplication are called in each instance. Therefore, the events of Application_OnStart and Application_OnEnd are called multiple times. In an ASP. NET
In application, only these events are shared in all HttpApplication instances.

Multithreading


If we use objects in the application range type, we should note that ASP. NET processes requests concurrently, so that the Application object can be accessed by multiple threads. Therefore, if the Code in the following example is accessed by different clients at the same time, it is dangerous and may not achieve the intended purpose. For ease of comparison, we listed the code written in three languages: VB, C #, and JScript:

C #


<%
Application ["counter"] = (Int32) Application ["counter"] + 1;
%>

VB

<%
Application ("counter") = CType (Application ("counter") + 1, Int32)
%>

JScript

<%
Application ("counter") = Int32 (Application ("counter") + 1 );
%>


To implement secure thread processing, we can use the Lock and Unlock methods to process this Application object. See the following code written in three languages: VB, C #, and JScript:


C #


<%
Application. Lock ();
Application ["counter"] = (Int32) Application ["counter"] + 1;
Application. UnLock ();
%>

VB

<%
Application. Lock ()
Application ("counter") = CType (Application ("counter") + 1, Int32)
Application. UnLock ()
%>

JScript

<%
Application. Lock ();
Application ("counter") = Int32 (Application ("counter") + 1 );
Application. UnLock ();
%>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.