blog

Home / DeveloperSection / Blogs / Multithreading with C#

Multithreading with C#

priyanka kushwaha10594 24-Feb-2015

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

Example:

Default.aspx

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

 

<!DOCTYPEhtml>

 

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

<headrunat="server">

    <title></title>

</head>

<body>

    <formid="form1"runat="server">

    <h4>Multi Threading Example</h4>

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

    </form>

</body>

</html>

  

Default.aspx.cs

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

{

    publicpartialclassDefault : System.Web.UI.Page

    {

        protectedvoid Page_Load(object sender, EventArgs e)

        {

            ThreadStart childthread = newThreadStart(ChildThreadCall);

            Response.Write("Child thread started");

            Thread child = newThread(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();

        }

        publicvoid ChildThreadCall()

        {

            try

            {

                lblMsg.Text = "Child thread started";

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

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

                {

                    Thread.Sleep(500);

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

                }

            }

            catch (ThreadAbortException ex)

            {

                lblMsg.Text += "child thread exception";

            }

            finally

            {

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

            }

           

        }

    }

}

Output: 

Multithreading with C#


Updated 24-Feb-2015

Leave Comment

Comments

Liked By