
- 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
Multithreading in C#
A thread is defined as the execution path of a program. Each thread defines a unique flow of contr.
In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
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 in C# −
Example
using System; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { Thread th = Thread.CurrentThread; th.Name = "MainThread"; Console.WriteLine("This is {0}", th.Name); Console.ReadKey(); } } }
Output
This is MainThread
- Related Articles
- Multithreading in Java
- Multithreading in C++
- Multithreading in C\n
- Deadlock in Java Multithreading
- What is multithreading in C#?
- Java Thread Priority in Multithreading
- What are multithreading models?
- Merge Sort using Multithreading in C++
- How to Implement Multithreading in Ruby
- Benefits of Multithreading in Operating System
- Difference Between Multitasking and Multithreading in OS
- What is a multithreading model in OS?
- Difference between Multiprocessing and Multithreading
- Why doesn't JavaScript support multithreading?
- Make strings in array become keys in object in a new array in JavaScript?
