Understanding Threading in ASP.NET

Threading is a technique which enables multiple tasks to be executed at the same time. In the .NET Framework, there are many types that enable you to easily and quickly add multi-threading to your .NET Web application.

In traditional ASP, developers faced more threading related issues. There are specific rules about the threading requirements of objects, as ASP is built on COM. The objects that are used by the ASP pages need to be apartment threaded for maximum efficiency. In ASP.NET, most of the threading requirements of objects are fulfilled, which solves threading related issues and allows developers to easily build .aspx pages.

ASP.NET uses multiple threads within each worker process to service requests. The Page requests are always serviced on the same thread. To service any new requests, a distinct instance of the Page class is always created. You can also use distinct instances of the application and module objects, to service each request. You need to know about threading to service requests, so that you do not make incorrect assumptions about the objects in your applications that may be accessed by concurrent threads. ASP.NET uses the process-wide Common Language Runtime (CLR) thread pool to service requests. You can configure the size of this pool in the processModel element of machine.config. Both the Worker threads and the I/O threads are set to 25, by default.

<processModel enable="true"
•••
maxWorkerThreads="25"
maxIoThreads="25" />

To use threads in your application, you can use an asynchronous handler. Most of the ASP.NET pages and handlers are serviced synchronously on threads drawn from the process-wide thread pool. To service requests asynchronously, you can create handlers. Asynchronous handlers implement the IHttpAsyncHandler interface, which derives from IHttpHandler implemented by synchronous handlers:

public interface IHttpAsyncHandler : IHttpHandler
{
IAsyncResult BeginProcessRequest(HttpContext ctx,
AsyncCallback cb,
object obj);
void EndProcessRequest(IAsyncResult ar);
}

Two additional methods need to be implemented by the handlers that implement this interface, other than the standard methods of IHttpHandler. The BeginProcessRequest method is called by the application class instead of directly calling the ProcessRequest. The handler launches a new thread to process the request and then immediately return from the BeginProcessRequest method, and passes back a reference to a valid IAsyncResult instance so that the runtime knows the operation is complete. When the request processing gets over, the EndProcessRequest method is called to clean up the allocated resources.

| What is Private Access Modifier in C#? |Introducing Microsoft SQL Server 2000 Desktop Engine | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.