Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Article

Introduction to Threads in C#

Rate me:
Please Sign up or sign in to vote.
2.48/5 (63 votes)
13 Apr 20042 min read 434.1K   38   17
A Beginers introduction to Threads using C#

Introduction

This article will give you basic idea about how to create a Thread in C# and how to work on the created Thread.

Introduction to Threads In C#

Threads: Threads are often called lightweight processes. However they are not process.es A Thread is a small set of executable instructions, which can be used to isolate a task from a process. Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications.

C# Thread:. . Net Framework has thread-associated classes in System.Threading namespace. The following steps demonstrate how to create a thread in C#.

Step 1. Create a System.Threading.Thread object.

Creating an object to System.Threading.Thread creates a managed thread in .Net environment. The Thread class has only one constructor, which takes a ThreadStart delegate as parameter. The ThreadStart delegate is wrap around the callback method, which will be called when we start the thread.

Step 2: Create the call back function

This method will be a starting point for our new thread. It may be an instance function of a class or a static function. Incase of instance function, we should create an object of the class, before we create the ThreadStart delegate. For static functions we can directly use the function name to instantiate the delegate. The callback function should have void as both return type and parameter. Because the ThreadStart delegate function is declared like this. (For more information on delegate see MSDN for “Delegates”).

Step 3: Starting the Thread.

We can start the newly created thread using the Thread’s Start method. This is an asynchronous method, which requests the operating system to start the current thread.

For Example:

C#
// This is the Call back function for thread.           

Public static void MyCallbackFunction()

{
while (true)

  {
  System.Console.WriteLine(" Hey!, My Thread Function Running");

………

  } 

}

public static void Main(String []args)

{

// Create an object for Thread

Thread MyThread = new Thread(new ThreadStart 
(MyCallbackFunction));

MyThread.Start()

……

}
Killing a Thread:

We can kill a thread by calling the Abort method of the thread. Calling the Abort method causes the current thread to exit by throwing the ThreadAbortException.
MyThread.Abort(); 

Suspend and Resuming Thread:

We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s Suspend and Resume methods.

MyThread.Suspend() // causes suspend the Thread Execution.
MyThread.Resume() // causes the suspended Thread to resume its execution.

Thread State:

A Thread can be in one the following state.


Thread State

Description

Unstarted

Thread is Created within the common language run time but not Started still.

Running

After a Thread calls Start method

WaitSleepJoin

After a Thread calls its wait or Sleep or Join method.

Suspended

Thread Responds to a Suspend method call.

Stopped

The Thread is Stopped, either normally or Aborted.


We can check the current state of a thread using the Thread’s ThreadState property.

Thread Priorty:

The Thread class’s ThreadPriority property is used to set the priority of the Thread. A Thread may have one of the following values as its Priority:  Lowest, BelowNormal, Normal, AboveNormal, Highest. The default property of a thread is Normal.

By Mohamed Hasan S.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead GE Healthcare
India India
Hey, I am mohamed hasan. Working as a software engineer in a India based Software Company.
Willing to learn new concepts and technologies.

Contact him at: hasansheik@gmail.com
hasansheik@yahoo.co.in

Comments and Discussions

 
GeneralMy vote of 4 Pin
Spasticus18-Apr-17 22:12
Spasticus18-Apr-17 22:12 
QuestionThread Problem Pin
pramodkumarw30-May-13 7:26
pramodkumarw30-May-13 7:26 
AnswerRe: Thread Problem Pin
Marco Bertschi7-Nov-13 2:42
protectorMarco Bertschi7-Nov-13 2:42 
GeneralMy vote of 4 Pin
shani_75926-Dec-12 19:44
shani_75926-Dec-12 19:44 
GeneralMy vote of 1 Pin
mlzg425-Jun-12 1:38
mlzg425-Jun-12 1:38 
Incorrect information.
GeneralRe: My vote of 1 Pin
Marco Bertschi7-Nov-13 2:43
protectorMarco Bertschi7-Nov-13 2:43 
GeneralMy vote of 3 Pin
Ilham Israfilov12-Apr-12 9:17
Ilham Israfilov12-Apr-12 9:17 
GeneralMy vote of 1 Pin
usernameCasper11-Jan-11 9:18
usernameCasper11-Jan-11 9:18 
GeneralPassing parameters Pin
Member 27242142-Mar-10 2:15
Member 27242142-Mar-10 2:15 
GeneralIt is a thread function not a callback PinPopular
Jon Burchel2-Jul-08 17:08
Jon Burchel2-Jul-08 17:08 
GeneralNeeds Threads URL. Pin
H.Mohamed zakir11-Dec-07 18:04
H.Mohamed zakir11-Dec-07 18:04 
GeneralRe: Needs Threads URL. Pin
Game-point27-Oct-09 1:15
Game-point27-Oct-09 1:15 
QuestionRegistring Callback Function Pin
shahid hameed3-Aug-07 3:15
shahid hameed3-Aug-07 3:15 
QuestionProblem in threading Pin
NVMehta21-Mar-07 6:46
NVMehta21-Mar-07 6:46 
AnswerRe: Problem in threading Pin
mhejle13-Sep-07 20:32
mhejle13-Sep-07 20:32 
AnswerRe: Problem in threading Pin
carlmalden30-Sep-08 7:01
carlmalden30-Sep-08 7:01 
GeneralRe: Problem in threading Pin
vCillusion2-Jun-18 12:59
vCillusion2-Jun-18 12:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.