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.
<%
Application("counter") = CType(Application("counter") + 1, Int32)
%>
VB
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:
<%
Application.Lock()
Application("counter") = CType(Application("counter") + 1, Int32)
Application.UnLock()
%>
VB
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.
- ASP.NET Framework applications consist of everything under one virtual directory of the Web server.
- You create an ASP.NET Framework application by adding files to a virtual directory on the Web server.
- The lifetime of an ASP.NET Framework application is marked by Application_Start and Application_End events.
- Access to application-scope objects must be safe for multithreaded access.
Copyright 2001 Microsoft Corporation. All rights reserved.