Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • ASP.NET Multithreading

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 44
    Comment on it

    While developing a project in ASP.NET there are situations where you need to make your task multi-threaded to run in the background or in the foreground.

    For ex : If your application is sending notification after each day you need to create a thread for that it will work in the background.

    Life Cycle of a thread:

    The Unstarted State : In this state start method will not be called but the instance of the thread will be built.

    The Ready State : In this state the thread will be ready for execution and waiting for CPU cycle.

    • The Not Runnable State : a thread is not runnable, when:

      • Sleep method has been called
      • Wait method has been called
      • Blocked by I/O operations
        • The Dead State : It is a situation when the thread has already completed execution or it has been aborted.

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="threaddemo._Default" %>
    
    
    
    <html  >
    
       <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>

     

     

    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";
             }
          }
       }
    }
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: