Tuesday, 21 August 2018

C#: isAlive: get the running status of the thread

Every thread has a property 'IsAlive', it returns True, if thread is not ends (thread can be in running, blocking (or) sleep state), else false.

Program.cs
using System.Threading;
using System;

namespace ThreadingTutorial
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(printX);
            t1.Start();

            Console.WriteLine("Is thread t1 alive {0}", t1.IsAlive);
            t1.Join();
            Console.WriteLine("\nIs thread t1 alive {0}", t1.IsAlive);

        }

        public static void printX()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("X");
            }
                
        }
    }
}

Sample Output
Is thread t1 alive True
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Is thread t1 alive False

Previous                                                 Next                                                 Home

No comments:

Post a Comment