Multi Threading in ASP.Net Assignment Help

Assignment Help: >> ASP.Net Programming >> Multi Threading in ASP.Net

Multi Threading in ASP.Net

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

Threads are lightweight processes. One general example of use of thread is implementation of active programming by modern operating systems. Use of threads save wastage of CPU cycle and gain efficiency of an application.

So far we have written application programs where a single thread executes as a single process which is the running instance of the application. However, that way the application may perform one job at a time. To build it execute more than one task at a time, it should be divided into smaller threads.

In .NET, the threading is operated through the System.Threading namespace. Preparing a variable of the System.Threading.Thread type gives you to make a new thread to start working with. It gives you to create and access single threads in a program.

Creating Thread:

A thread is prepared by creating a Thread object, calling its constructor a ThreadStart reference.

ThreadStart childthreat = new ThreadStart(childthreadcall);

The Thread Life Cycle:

The life cycle of a thread executes when an object of the System.Threading.Thread class is build and ends when the thread is completes execution or terminated.

Following are the several states in the life cycle of a thread :

  • The Unstarted State: it is the situation when the part of the thread is prepared but the Start function has not been called.
  • The Ready State: it is the situation when the thread is ready to execute and waiting CPU cycle.
  • The Not Runnable State: a thread is not executable, when:
    • Sleep method has been involed
    • Wait method has been invoked
    • Blocked by I/O operations
  • The Dead State: it is the situation when the thread has finished execution or has been aborted.

The Thread Priority:

The Priority property of the Thread class denotes the priority of one thread with respect to other. The .Net executiontime selects the ready thread with the highest priority.

The priorities would be defined as:

  • Above normal
  • Below normal
  • Highest
  • Lowest
  • Normal

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

NewThread.Priority = ThreadPriority.Highest;


Example:

The given example explains the uses of the Thread class. The page has a label control for showing messages from the child thread. The messages from the main method is directly shown using the Response.Write() function. So it seems on the top of the page.

The source file is as given:

<%@ Page Language="C#"

         AutoEventWireup="true"

         CodeBehind="Default.aspx.cs"

         Inherits="threaddemo._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <h3>Thread Example</h3>

    </div>

    <asp:Label ID="lblmessage" runat="server" Text="Label">

    </asp:Label>

    </form>

</body>

</html>

The code behind file is as given:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Threading;

 

namespace threaddemo

{

   public partial class _Default : System.Web.UI.Page

   {

      protected void Page_Load(object sender, EventArgs e)

      {

         ThreadStart childthreat =

                          new ThreadStart(childthreadcall);

         Response.Write("Child Thread Started <br/>");

         Thread child = new Thread(childthreat);

         child.Start();

         Response.Write(

            "Main sleeping  for 2 seconds.......<br/>");

         Thread.Sleep(2000);

         Response.Write(

            "<br/>Main aborting child thread<br/>");

         child.Abort();

      }

      public void childthreadcall()

      {

      try{

         lblmessage.Text = "<br />Child thread started <br/>";

         lblmessage.Text += "Child Thread: Coiunting to 10";

         for( int i =0; i<10; i++)

         {

            Thread.Sleep(500);

            lblmessage.Text += "<br/> in Child thread </br>";

         }

         lblmessage.Text += "<br/> child thread finished";

      }

      catch(ThreadAbortException e)

      {

         lblmessage.Text += "<br /> child thread - exception";

      }

      finally{

         lblmessage.Text += "<br /> child thread

                           - unable to catch the exception";

      }

      }

   }

}

Observe the following:

  • When the page is started, a new thread is executed with the reference of the function childthreadcall(). The main thread activities are shown directly on the web page.
  • The second thread executes and transmit messages to the label control.
  • The main thread sleeps for 2000 ms, in the mean while which the child thread runs.
  • The child thread executes till it is aborted by the main thread. It increases the ThreadAbortException and is ended.
  • Control gives to the main thread.

When executed the application program transmit the given messages:

1450_multi threading.png

 

 

Email based ASP.Net assignment help - homework help at Expertsmind

Are you searching ASP.Net expert for help with Multi Threading in ASP.Net questions?  Multi Threading in ASP.Net topic is not easier to learn without external help?  We at www.expertsmind.com offer finest service of ASP.Net assignment help and ASP.Net homework help. Live tutors are available for 24x7 hours helping students in their Multi Threading in ASP.Net related problems. Computer science programming assignments help making life easy for students. We provide step by step Multi Threading in ASP.Net question's answers with 100% plagiarism free content. We prepare quality content and notes for Multi Threading in ASP.Net topic under ASP.Net theory and study material. These are avail for subscribed users and they can get advantages anytime.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving ASP.Net queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd