C# Programs on Threads

C# Programming Examples - Threads

A thread is a path in execution. It contains the program counter, thread ID, stack, and set of registers. Threads are lightweight processes. Multithreading is the most useful feature of C# that allows two or more parts of a program to be programmed simultaneously to maximize CPU efficiency. To create a multithreaded application in C#, we need to use the System.Threading namespace. C# supports two types of threads: foreground thread and background thread.

C# Thread Life Cycle:

Each thread in C# has a life cycle. The thread life cycle begins when an instance of the System.Threading. class is created. When a thread completes its task execution, its life cycle is complete. Here are the various states in the thread life cycle:

  • Unstarted State
  • Ready State
  • Runnable State
  • Not Runnable State
  • Dead State

C# Thread Properties

Here is a list of the important properties of the Thread class:

advertisement
advertisement
  • Name: It is used to get or set the current thread name.
  • Priority: It is used to get or set the current thread priority.
  • IsAlive: It checks whether the current thread is alive or not.
  • CurrentThread: It is used to get the current running thread.
  • ThreadState: It is used to return a value that represents the thread’s current state.

C# Thread Methods

Here is a list of the important methods of the Thread class:

  • Abort(): It throws ThreadAbortException and it is used to terminate the thread.
  • Join(): It is used to block all calling threads until this thread terminates.
  • Interrupt(): It is used to interrupt a thread.
  • Resume(): It returns a suspended thread to its previous state.
  • Suspend(): It suspends the current thread if it is not suspended.
  • Start(): It changes the thread’s current state to Runnable.

The following section contains C# programs on threads, thread methods, and thread pools. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.

Here is the listing of C# programming examples on Thread:

  1. C# Programs on Thread Basics
  2. C# Programs on Thread Methods

1. C# Programs on Thread Basics

Program Description
Simple Thread Program in C# C# Program to Create a Simple Thread
Pause a Thread in C# C# Program to Pause a Thread
Kill a Thread in C# C# Program to Kill a Thread
Thread Lock in C# C# Program to Demonstrate Lock in Thread
Thread Pools in C# C# Program to Create Thread Pools

advertisement

2. C# Programs on Thread Methods

Program Description
Get Thread Name in C# C# Program to Print the Name of the Current Thread
Naming a Thread using Name Property in C# C# Program to Assign Name to Thread by using Name Property
Thread.sleep() Method in C# C# Program to Implement Sleep Method of Thread
Monitor Lock in C# C# Program to Demonstrate Monitor Lock with Lock Statement
Get Current Thread Context Id in C# C# Program to Find the Current Context Id of the Thread
Thread Priority in C# C# Program to Show the Priority in Threads
Check Status of Current Thread in C# C# Program to Check Status of the Current Thread
Pass Parameter to Thread in C# C# Program to Demonstrate the Concept of Passing Parameter for Thread

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.