---
title: "Multithreading with C#"  
description: "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 execut"  
author: "priyanka kushwaha"  
published: 2015-02-24  
updated: 2015-02-24  
canonical: https://www.mindstick.com/blog/784/multithreading-with-c-sharp  
category: ".net"  
tags: ["c#", "asp.net", "thread", "multiple threading"]  
reading_time: 2 minutes  

---

# Multithreading with C#

This blog explains how [multithreading](https://www.mindstick.com/articles/11979/multithreading-with-the-backgroundworker-component-in-asp-dot-net) works in c#.

What is a thread?

A thread can be defined as a semi-process with a definite [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) point, an [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) [sequence](https://www.mindstick.com/interview/34504/difference-between-identity-vs-sequence) and a terminating point. It maintains its own stack where it keeps the [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [handlers](https://www.mindstick.com/forum/1388/button-handlers-in-xaml), the [scheduling](https://www.mindstick.com/forum/157665/explain-the-round-robin-scheduling-algorithm-with-an-example-in-os) priority and other details that the system might need to re-activated that thread.

System.[Threading](https://www.mindstick.com/blog/187/threading-in-c-sharp). 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](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) Start() again.

\
Thread Priority:

System.Threading.Thread.Priority [enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) defines the priority states of a thread.

1. Highest

2. AboveNormal

3. Normal

4. BelowNormal

5. Lowest

Example:

##### Default.aspx

```
<%@ 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">    <title></title></head><body>    <form id="form1" runat="server">    <h4>Multi Threading Example</h4>        <asp:Label ID="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{    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);            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            {                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#](https://www.mindstick.com/blogs/308a16f6-c007-4093-9e13-403ab97b6e5c/images/72394d4e-d667-4aa4-9b2d-802e32c6688e.png)

---

Original Source: https://www.mindstick.com/blog/784/multithreading-with-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
