C# .NET - Problem Regarding Application.lock and Unlock method Pls tell me the process involved in this.

Asked By ramana reddy
30-Nov-06 05:02 AM
end of post

HttpApplicationState class  HttpApplicationState class

30-Nov-06 05:12 AM

The Application Object (HttpApplicationState class) provides two methods, Lock and Unlock, that allow only one thread at a time to access application-state variables.

Calling Lock on the Application object causes ASP.NET to block attempts by code running on other worker threads to access anything in application state. These threads are unblocked only when the thread that called Lock calls the corresponding Unlock method on the Application object.

please see this example,

// C# code from within a page, a handler, or Global.asax.
Application.Lock();
Application["SomeGlobalCounter"] =
   (int)Application["SomeGlobalCounter"] + 1;
Application.UnLock();

you lock your application before incrementing this counter variable and unlock it once its done,

If you do not explicitly call Unlock, the .NET Framework automatically removes the lock when the request completes, when the request times out, or when an unhandled error occurs during request execution and causes the request to fail. This automatic unlocking prevents the application from deadlocking.

This is taken from http://msdn2.microsoft.com/en-us/library/bf9xhdz4(VS.71).aspx

Understanding Applications and State  Understanding Applications and State

30-Nov-06 05:31 AM

Understanding Applications and State

What is an ASP.NET Application?

ASP.NET defines an application as the sum of all files, pages, handlers, modules, and executable code that can be invoked or run in the scope of a given virtual directory (and its subdirectories) on a Web application server. For example, an "order" application might be published in the "/order" virtual directory on a Web server computer. For IIS the virtual directory can be set up in the Internet Services Manager; it contains all subdirectories, unless the subdirectories are virtual directories themselves.

Each ASP.NET Framework application on a Web server is executed within a unique .NET Framework application domain, which guarantees class isolation (no versioning or naming conflicts), security sandboxing (preventing access to certain machine or network resources), and static variable isolation.

ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for managing the entire lifetime of the request and is reused only after the request has been completed. This means that user code within the HttpApplication does not need to be reentrant.

Creating an Application

To create an ASP.NET Framework application you can use an existing virtual directory or create a new one. For example, if you installed Windows 2000 Server including IIS, you probably have a directory C:\InetPub\WWWRoot. You can configure IIS using the Internet Services Manager, available under Start -> Programs -> Administrative Tools. Right-click on an existing directory and choose either New (to create a new virtual directory) or Properties (to promote an existing regular directory).

By placing a simple .aspx page like the following in the virtual directory and accessing it with the browser, you trigger the creation of the ASP.NET application.

function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } td.code { padding-left:10px; padding-right:10px; padding-top:0px; padding-bottom:0px; border-left: 1px solid #B1B1B1; border-bottom: 1px solid #DADADA; border-top: none; border-right: 1px solid #DADADA; } td.tab { text-align:center; font: verdana; width:15%; border-top: 1px solid #B1B1B1; border-bottom: none; border-left: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #F0F0F0; padding: 3px; } td.backtab { text-align: center; font: verdana; width: 15%; border-top: 1px solid #B1B1B1; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #E3E3E3; padding: 3px; } td.space { width:70%; font: x-small verdana; padding: 0px 0px 0px 0px; border-top: none; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; background: white; }

C# VB  
		
<%@Page Language="C#"%>
<html>
<body>
<h1>hello world, <% Response.Write(DateTime.Now.ToString()); %></h1>
</body>
</html>

Now you can add appropriate code to use the http://dotnetjunkies.com/QuickStartv20/aspnet/doc/applications/stateoverview.aspx object--to store objects with application scope, for example. By creating a http://dotnetjunkies.com/QuickStartv20/aspnet/doc/applications/globalasax.aspx file you also can define various event handlers-- for the Application_Start event, for example.

Lifetime of an Application

An ASP.NET Framework application is created the first time a request is made to the server; before that, no ASP.NET code executes. When the first request is made, a pool of HttpApplication instances is created and the Application_Start event is raised. The HttpApplication instances process this and subsequent requests, until the last instance exits and the Application_End event is raised.

Note that the Init and Dispose methods of HttpApplication are called per instance and thus can be called several times between Application_Start and Application_End. Only these events are shared among all instances of HttpApplication in one ASP.NET application.

A Note on Multiple Threads

If you use objects with application scope, you should be aware that ASP.NET processes requests concurrently and that the Application object can be accessed by multiple threads. Therefore the following code is dangerous and might not produce the expected result, if the page is repeatedly requested by different clients at the same time.

function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } td.code { padding-left:10px; padding-right:10px; padding-top:0px; padding-bottom:0px; border-left: 1px solid #B1B1B1; border-bottom: 1px solid #DADADA; border-top: none; border-right: 1px solid #DADADA; } td.tab { text-align:center; font: verdana; width:15%; border-top: 1px solid #B1B1B1; border-bottom: none; border-left: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #F0F0F0; padding: 3px; } td.backtab { text-align: center; font: verdana; width: 15%; border-top: 1px solid #B1B1B1; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #E3E3E3; padding: 3px; } td.space { width:70%; font: x-small verdana; padding: 0px 0px 0px 0px; border-top: none; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; background: white; }

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

To make this code thread safe, serialize the access to the Application object using the Lock and UnLock methods. However, doing so also means accepting a considerable performance hit:

function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } td.code { padding-left:10px; padding-right:10px; padding-top:0px; padding-bottom:0px; border-left: 1px solid #B1B1B1; border-bottom: 1px solid #DADADA; border-top: none; border-right: 1px solid #DADADA; } td.tab { text-align:center; font: verdana; width:15%; border-top: 1px solid #B1B1B1; border-bottom: none; border-left: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #F0F0F0; padding: 3px; } td.backtab { text-align: center; font: verdana; width: 15%; border-top: 1px solid #B1B1B1; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; cursor: hand; background: #E3E3E3; padding: 3px; } td.space { width:70%; font: x-small verdana; padding: 0px 0px 0px 0px; border-top: none; border-right: none; border-bottom: 1px solid #B1B1B1; border-left: 1px solid #B1B1B1; background: white; }
C# VB  
		
<%
Application.Lock()
Application("counter") = CType(Application("counter"), Int32) + 1
Application.UnLock()
%>

Another solution is to make the object stored with an application scope thread safe. For example, note that the collection classes in the System.Collections namespace are not thread safe for performance reasons.

reference

http://dotnetjunkies.com/QuickStartv20/aspnet/doc/applications/default.aspx

-------------------------------------------

Aplication object

The Application object provides a mechanism for storing data that is accessible to all code running within the Web application, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.

[c#]

Application.Lock();
Application["mydata"]="mydata";
Application.UnLock();
reference
http://www.programmersheaven.com/2/State-Management-in-ASP-NET
http://msdn2.microsoft.com/en-us/library/bf9xhdz4(VS.71).aspx

Try this one  Try this one

08-Dec-06 01:32 AM
Client-side state management
There is no information maintained on the server between round trips. Information will be stored in the page or on the client’s computer.
1. Cookies
2. Hidden Field
3. View State
4. QyeryString
 
Server-side state management:
Information will be stored on the server, it has higher security but it can use more http://www.csharphelp.com/archives/archive207.html# resources.
 
 
 
The Application object provides a mechanism for storing data that is accessible to all code running within the http://www.csharphelp.com/archives/archive207.html#, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.
[c#]
Application.Lock();
Application[“mydata”]=”mydata”;
Application.UnLock();

Create New Account
help
using dll with C# asp.net application .NET Framework Hello! Assume I have four C# asp.net application running on the same computer. These four C# asp.net application is using two dll named a.dll and b.dll Assume also that these four
data connection of .net Framework & .net Compact Framework .NET Framework Hi, I'm planning to create a database apllication in C# using .NET Framework. But I would also like to create a .NET Compact Framework application where it shares
which net framework version can I uninstall ?Hi, Windows 7 I have the following version of .net framework on my pc : .NET Framework 1 .NET Framework CRT .NET Framework WinForms .NET Framework 2 .NET Framework ASP .NET .NET Framework CA Dr
net framework sp1 keeps loading on top of .net framework .NET Framework A week ago I my computer was running slowly & my uniblue power suite informed me I did. After I had cleaned my registry with uniblue registry I noticed that my .net framework 1.1 was showing nil in my add & remove programs. I then tried to re
ANN: Bricksoft IM(MSN) SDK For .NET Framework / .NET Compact Framework released! Windows 7 IM SDK For .NET Framework / .NET Compact Framework provide connectivity with the MSN Messenger service. The library is built in C# and can