Thread Life Cycle in C#
A Thread is a separate sequence of instructions designed for performing a specific task in the program. A task may represent an operation such as reading data, sending a file over the internet, printing some results or performing certain calculations. Multi-threading means performing multiple tasks at the same time during the execution of a program.
Multithreading in C#:
The execution of a C# program starts with a single thread called main thread. The Common Language Runtime (CLR) and the operating system run it automatically. We can create other threads for performing desired tasks in the program. The process of developing a program for execution with multiple threads say Multithreaded programming and the process of execution says Multithreading.
C# defines a namespace known as
System. Threading contains classes and interfaces that are required for developing and running multithreaded programs. It has the following syntax:
using System. Threading;
The classes and interfaces are contained in the
System.Threading namespace allows us to perform multithreading in C#. We can use the methods and properties contained in these classes to perform tasks such as – Synchronizing the activities of a thread and creating a thread.
Thread Life Cycle:
In C#, the thread life cycle specifies the current status of the thread. The thread automatically switches from one state to another state. At some times, you can switch its state by using the methods offered by Thread class.
1. Ready: It is the initial state. The thread that has just created.
2. Running: The thread is currently being executed.
3. Sleeping: The thread is temporarily paused. The .NET framework offers automatic switching between Running and Sleeping states when other threads are executed.
4. Suspended: The thread is temporarily suspended. It will continued, when you call Resume() method.
5. Dead: The execution of the thread is finished.
The Thread class helps us to perform tasks such as creating and setting the priority of a thread. We can use this class to control a thread and obtain its status. The thread class provides various properties that allow us to perform tasks such as obtaining the status of a thread and specifying a name for the thread.
1. CurrentThread: It retrieves the name of the thread which is currently running.
2. IsAlive: It retrieves a value to indicate the current state of thread execution. The value of the IsAlive property is true if the thread has been started or has been terminated, otherwise, the value is false.
3. Name: It specifies a name for a thread.
4. Priority: To obtain a value which indicates the scheduling priority of a thread. By default, the value of the Priority property is Normal. We can assign either Highest, AboveNormal, Normal, BelowNormal or Lowest value to the priority property.
Methods of the Thread Class:
1. Interrupt: To interrupt the thread which is in the WaitSleepJoin state.
2. Join: To block a thread until another thread has terminated.
3. Resume: To resume a thread which has been suspended earlier.
4. Sleep: To block the current thread for a specified time period.
5. SpinWait: To make a thread wait the number of times specified in the iteration parameter.
6. Start: To start a thread
7. Suspend: To suspend a thread.