Home > DeveloperSection > Blogs > Multithreading with C#

Multithreading with C#

.NET C#  ASP.Net  Thread  Multiple Threading 
2 Comment(s)
 1229  View(s)
Rate this:

Multithreading with C#

This blog explains how multithreading works in c#.

What is a thread?

A thread can be defined as a semi-process with a definite starting point, an execution sequence and a terminating point. It maintains its own stack where it keeps the exception handlers, the scheduling priority and other details that the system might need to re-activated that thread.

System.Threading. Thread is the main  class for creating threads and controlling them. The thread class has a number of methods.

1.       Start(): starts the execution of the  thread.

2.       Suspend(): suspends the thread.

3.       Resume: resumes a thread that has been suspend.

4.       Interrupt: interrupts a thread that is in the wait, sleep or join stage.

5.       Join(): blocks  a calling thread until the thread terminates.

6.       Sleep(int x): suspends the thread for specified amount of time.

7.       Abort(): Begins the process of terminating the thread. Once  the thread terminates, it cannot be restarted by calling the function Start() again.


Thread Priority:

System.Threading.Thread.Priority enumeration defines the priority states of a thread.

1.       Highest

2.       AboveNormal

3.       Normal

4.       BelowNormal

5.       Lowest



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ThreadingExample.Default" %>


<!DOCTYPE html>


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

<head runat="server">




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

    <h4>Multi Threading Example</h4>

        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>







using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Threading;

namespace ThreadingExample


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


        protected void Page_Load(object sender, EventArgs e)


            ThreadStart childthread = new ThreadStart(ChildThreadCall);

            Response.Write("Child thread started");

            Thread child = new Thread(childthread);


            Response.Write("Main thread is sleeping for 1 second...");


            Response.Write("Main Thread is aborting child thread");



        public void ChildThreadCall()




                lblMsg.Text = "Child thread started";

                lblMsg.Text += "Child Thread is counting to 5....";

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



                    lblMsg.Text += "we are in child thread";



            catch (ThreadAbortException ex)


                lblMsg.Text += "child thread exception";




                lblMsg.Text += "Unable to catch to  exception";








By Milind narkhede on   2 years ago
Very Nice............


By John Smith on   2 years ago
keep posting Priyanka.. .Good Work!

Don't want to miss updates? Please click the below button!

Follow MindStick