top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Multi-Threading in ASP.NET?

0 votes
175 views
Multi-Threading in ASP.NET?
posted Feb 29, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

In these days, almost all programmers need to understand multi-threading but unfortunately many of the programmers don't know about and not even try to learn it. So in this article, I will explain multi-threading and how multi-threading works.

To learn about multi-threading, we first need to learn what is a thread. A thread is actually defined as the execution path of a program. Any one thread follows program flow for wherever it is in the code that means every thread identifies the unique flow of control. There was always one thread running for each process in an operating system before multi-threading. So programs where a single thread runs as a single process which is the running instance of the application. In this way, an application can do only one job at the time.

Multi-threading is a technique that can be used to perform time consuming tasks in a separate new thread rather than the main thread. It can be seen as two threads, the main thread and the newly created thread are running in parallel. It will improve the performance and responsiveness of your application. For example if we have a time consuming function and we want to call this function on a response of an event, we may assign this function to a new thread rather than existing main application thread. So the main thread will not be blocked by this time consuming function and newly created thread will run at the time in the background.

ASP.NET is designed to support multi-threading from the start. Web servers make extensive use of concurrency by introduction of multiple processes or by generating multiple threads to service multiple requests and perform the work. ASP.NET uses the same technique of multiple threads within each worker process to service multiple requests. Web developers has no concerns about their issues regarding multi-threading and getting performance by using multi-threading because ASP.NET take care of all the multi-threading. All page requests are serviced on the same thread and any separate instance of the page class is created to service new requests.

ASP.NET encourages two main ways of multi-threading if programmer want to use multi-threading by itself. These are: starting your own threads with ThreadStart delegates, and using the ThreadPool class either directly or indirectly by using asynchronous methods. You may create a new thread for long jobs and may use the thread pool for short tasks.

The life cycle of a thread starts when an object of the Thread class is created and ends when the thread is terminated or completes its execution. 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. Once a thread is created its priority can be set by using the Priority property of the thread class.

1.Open Visual Studio 2012 Add New Empty Web Site
2.Add New Item (Default.aspx) and add code below in Default.aspx

<h4>Multi-Threading Exampleh4>
<asp:Label ID="lblmessage" runat="server" Text="Label">
asp:Label>

3.Write below code in code file

C#

using System.Threading;
protected void Page_Load(object sender, EventArgs e)
{

  ThreadStart childthread = new ThreadStart(ChildThreadCall);
  Response.Write("Child Thread Started 
");

  Thread child = new Thread(childthread);
  child.Start();
  Response.Write("Main Thread is sleeping for 1 second...
");
  Thread.Sleep(1000);
  Response.Write("
Main Thread is aborting child thread
");
  child.Abort();

}

public void ChildThreadCall()
{
    try
    {
       lblmessage.Text = "Child thread started 
";
       lblmessage.Text += "Child Thread is Counting to 5";

       for( int i =0; i<5; i++)
       {
          Thread.Sleep(500);
          lblmessage.Text += "
 we are in Child thread 
";
       }
       lblmessage.Text += "
 child thread completed";
    }
    catch(ThreadAbortException e)
    {
       lblmessage.Text += " child thread exception";
    }
    finally
    {
       lblmessage.Text += " Unable to catch the exception";
    }
 }

VB.NET

Imports System.Threading
Protected Sub Page_Load(sender As Object, e As EventArgs)

    Dim childthread As New ThreadStart(AddressOf ChildThreadCall)
    Response.Write("Child Thread Started 
")

    Dim child As New Thread(childthread)
    child.Start()
    Response.Write("Main Thread is sleeping for 1 second...
")
    Thread.Sleep(1000)
    Response.Write("
Main Thread is aborting child thread
")
    child.Abort()

  End Sub

  Public Sub ChildThreadCall()
    Try
      lblmessage.Text = "Child thread started 
"
      lblmessage.Text += "Child Thread is Counting to 5"

      For i As Integer = 0 To 4
        Thread.Sleep(500)
        lblmessage.Text += "
 we are in Child thread 
"
      Next
      lblmessage.Text += "
 child thread completed"
    Catch e As ThreadAbortException
      lblmessage.Text += " child thread exception"
    Finally
      lblmessage.Text += " Unable to catch the exception"
    End Try
  End Sub

4.Press F5 and see the result

answer Feb 29, 2016 by Shivaranjini
...