![]() |
VOOZH | about |
A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsAlive to check if the thread is alive or not. In other words, the value of this property indicates the current execution of the thread.
Example 1: This example demonstrates how to check the state of the main thread using the IsAlive property.
Is the main thread alive? : True
Explanation: In this example, we check the state of the main thread using Thread.CurrentThread. The IsAlive property returns true because the main thread is alive as long as the program is running.
public bool IsAlive { get; }
Return Type: This property returns "true" if the thread is started and not terminated normally or aborted, otherwise, return "false". The return type of this property is bool (boolean).
Example 2: This example demonstrates how to check whether custom threads (not the main thread) are alive by using the IsAlive property.
Is thread 1 is alive : False Is thread 2 is alive : False Is thread 1 is alive : True Is thread 2 is alive : True
Explanation: In the above example,
Note: If you want to ensure the threads have completed their execution before checking their status again, you could use T1.Join() and T2.Join() to block the main thread until both threads finish executing.