
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Threads in C#
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.
The following are the various states in the life cycle of a thread −
The Unstarted State - It is the situation when the instance of the thread is created but the Start method is not called.
The Ready State - It is the situation when the thread is ready to run and waiting CPU cycle.
The Not Runnable State - A thread is not executable, when
- Sleep method has been called
- Wait method has been called
- Blocked by I/O operations
The Dead State - It is the situation when the thread completes execution or is aborted.
The following is an example showing how to create a thread −
Example
using System; using System.Threading; namespace Demo { class Program { public static void ThreadFunc() { Console.WriteLine("Child thread starts"); } static void Main(string[] args) { ThreadStart childref = new ThreadStart(ThreadFunc); Console.WriteLine("In Main: Creating the Child thread"); Thread childThread = new Thread(childref); childThread.Start(); Console.ReadKey(); } } }
Output
In Main: Creating the Child thread Child thread starts
- Related Articles
- How to destroy threads in C#?
- Threads and Thread Synchronization in C#
- Difference Between Daemon Threads and User Threads In Java
- Find memory conflicts among multiple threads in C++
- User-level threads and Kernel-level threads
- Print 1 2 3 infinitely using threads in C
- Joining Threads in Java
- Synchronizing Threads in Python
- Killing threads in Java
- Using Threads in Rust Programming
- Threads vs Processes in Linux
- How to handle Python exception in Threads?
- Minimum and Maximum Priority Threads in Java
- How to use multiple threads in android?
- Maximum number of threads that can be created within a process in C
