Multithreading in Asp.Net

Introduction :

In this article we will discuss about Multithreading programming. Sometimes you need to perform a long time task, that uses intensive CPU operations. Sometimes you are acquiring a database and waiting for the results. Not to mention when we are performing an intensive I/O operations. As a result our, application will be dead with no response until this time consuming operation completes.

A common solution to this  problem is the use of Multithreading in Asp.NetThe Dot Net framework provides you with a class library that allows you to overcome this problem by using Multithreading .

Create Thread:

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

                 ThreadStart childthreat = new ThreadStart(childthreadcall);

Thread Priority:

The Priority property is defined by Thread class specifies the priority of one thread with respect to other. The  Dot Net runtime selects the ready thread with the highest priority.

Once a thread is created, its priority is set using the Priority property of the thread class.

 

Multithreading in Asp.Net:

Multi threading is a method which can be used when a time consuming task is making the central processing unit idle for a long time when there is some i/o interrupt and more threads are waiting in a queue .

Basically multi threading is a technique to run the threads parallalely  whenever there is some interrupt of the threads, then apart from waiting, cpu can process another thread, which makes the higher efficiency of cpu.

Example of Multithreading in Asp.Net:

protected void Page_Load(object sender, EventArgs e)

{

ThreadStart ts=mew ThreadStart(childcall);

Response.Write(“Start of child thread<br/>”);

Thread child1 = new Thread(ts);

child1.Start();

Thread.Sleep(1000);

Response.Write(“Thread is aborting<br/>”);

child1.Abort();

}

public void ChildCall()

{

try

{

lbl1.Text=”Child Thread”;

}

catch(ThreadAbortException e)

{

lbl1.Text=Child thread is aborted<br/>”;

}

finally

{

lbl1.Text=”Exception were not cached by the child thread<br/>”;

}

}

OutPut:

 image3