• subscribe
December 01, 2009 02:59 PM

Multithreading in ASP.NET Applications

Learn how you can improve application responsiveness
Dev Pro
InstantDoc ID #125367

Performance and responsiveness are key factors for an application's success. Multithreading, which you can use to boost application responsiveness, can overcome potential problems that come up due to long-running tasks or CPU-intensive operations. In this article, I explain how you can use multithreading in ASP.NET applications. To begin, let’s take a look at related concepts and terminologies.

Threads, Processes, and Tasks—What Are They and How Do They Differ?
A thread—also called a lightweight process—is the smallest individually schedulable entity within a process, and the path of execution within a process. A process on the other hand is the running instance of a program. An application’s main thread is the application thread. All other threads that you create in the application are called worker threads. A task is an enhanced form of a thread.

Multithreading, Multitasking, and Multiprocessing
Multithreading is the OS’s ability to have at the same point in time multiple threads in memory that switch between tasks in such a way as to provide a pseudo parallelism, as if all the threads are running simultaneously. Note that switching between threads involves a context switch, which in turn involves saving the current thread’s state, flushing the CPU, and handing control of the CPU to the next thread in the queue.  

Multithreading can be of the following types:
•    Serial – supported by DOS OS
•    Cooperative – supported by Windows 3.11
•    Preemptive – supported by Windows 9x, NT, XP, and so on

Multitasking is an enhanced form of multithreading. Note that all multitasking operating systems are also multithreading in nature and the reverse is also implied.  Multiprocessing is a concept that involves multiple processors, with each processor executing a particular thread at any point in time. Note that you can have only one thread in execution state in a single processor because a single CPU can execute only one thread at a time.

Advantages and Disadvantages of Multithreading
Using multithreading in applications boosts the application’s responsiveness. You can use multithreading to enhance the CPU and memory utilization in your system and provide support for concurrency. However, it’s difficult to test and debug applications that use multithreading. Moreover, implementing multithreading applications is quite complex.

Thread States
A thread can have one of the following states:

• Ready or Run-able state—A thread in this state has all resources it needs, excluding the processor. It awaits its turn in the ready queue (a queue of ready-to-use threads) to be allocated the processor.
• Running state—A thread in this state has all resources it needs, inclusive of the processor. Hence, a thread in this state is said to be in the running or executing state. Note that in a single processor, you can at any point in time have only one thread in the running state.
• Wait state—A thread in this state waits for the IO to be complete. Once the IO activity for the thread is complete, the thread is scheduled again to the ready queue.

The ThreadState enum in the System.Threading namespace in C# contains the various supported thread states in .NET.  The following are the members of the ThreadState enum:

• Unstarted
• Running
• Background
• StopRequested
• Suspended
• SuspendRequested
• WaitSleepJoin
• Aborted
• AbortRequested
• Stopped

Using the System.Threading.Thread Class
You can use the Thread class belonging to the System.Threading namespace to create, use, and manage threads in ASP.NET applications. To create and start a new thread, you need to create an instance of the Thread class and then call its start method. Here’s an example:

 

Thread thread = new Thread(new ThreadStart(DoWork));
thread.Start();

Notice the parameter DoWork in the call to the ThreadStart constructor in the code above. The method DoWork is a thread method. A thread method is one that contains the actual code that the thread should execute. It should have no parameters and should have a return type of void. Here’s what a typical thread method looks like:

 

public void DoWork()
{
while(some condition)
{
//Some code
}
}



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here