Multithreading in Asp.Net


Introduction:

A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations such as database access or some intense I/O operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.

Thread:

A thread is a basic unit to which the operating system allocates the processor time. It is an independent execution within the program. User can perform multiple tasks at a same time using threads in a program. A single threaded application can perform only one task at a time.

User has to wait for task to complete before another task is started. Multithreaded process executes one or more task at one time.

Thread Life Cycle:

The life cycle of a thread starts when an object of the System.Threading.Thread class is created. The life cycle of the thread ends when the task is completed. There are various states in the life cycle of the thread. The states are as mentioned below:
1) The Unstarted state
2) The Runnable state
3) The Not Runnable state
4) The Dead state

What Is Multithreading?

Multithreading is a technique that can be used to perform time consuming tasks in a separate additional thread other than the main application thread.


When you, for example, have a time-consuming function, you may need to call this function as a response of a button click. Now, instead of freezing all your application waiting for this function to return / to finish, you can create a new thread and assign this function to.

 When you do this, your application interface will not be blocked and you can use it to perform other tasks. At the same time, your time-consuming task is being carried out in the background.

Creating Thread:

A thread is created by creating a Thread object, giving its constructor a ThreadStart reference.

Thread Priority:

The Priority property of the Thread class specifies the priority of one thread with respect to other. The .Net runtime selects the ready thread with the highest priority.
The priorities could be categorized as:
  1. Above normal
  2. Below normal
  3. Highest
  4. Lowest
  5. Normal

Advantages and Disadvantages of Using Multithreading:

Despite improving your application's performance, and avoiding unresponsive user interface, multithreading has the following disadvantages:
  1. There is a runtime overhead associated with creating and destroying threads. When your application creates and destroys threads frequently, this overhead affects the overall application performance.
  2. Having too many threads running at the same time decreases the performance of your entire system. This is because your system is attempting to give each thread a time slot to operate inside.
  3. You should design your application well when you are going to use multithreading, or otherwise your application will be difficult to maintain and extend.

Also Read:

  1. Three Tier Architecture in Asp.Net
  2. Mvc introduction – Asp.Net Overview
  3. State Management in Asp.Net

Reference:

Multithreading in Asp.Net


Comments

Popular posts from this blog

Top25 ASP.NET Interview Questions - FAQ | Crack the Interview Easily

State Management in ASP.Net:

The Future of .NET Developement