In this example we will discuss the thread execution and isalive property of thread. As you can check in output before starting the thread IsAlive is false and after staring thread IsAlive property is True and After Aborting the therad IsAlive property is Flase. So after aborting the thread is no more.
While executing this program sometimes you can get that even after aborting the thread IsAlive property is true. This happened because thread abort take time and the next statement executes before thread completely abort. So while programming please take care of this scenrio.
Thread IsAlive Property and Thread Execution Example:
using System;
using System.Text;
using System.Threading;
namespace Console_App
{
public class Class1
{
public void fun1()
{
for (int i = 0; i < 10000; i++)
{
//Console.WriteLine(" #" + i );
// Thread.Sleep(2);
}
}
}
public class Class2
{
public static void Main()
{
Class1 a = new Class1();
Thread thread1 = new Thread(new ThreadStart(a.fun1));
Console.WriteLine("Before Starting Thread: " + thread1.IsAlive);
thread1.Start();
Console.WriteLine("After Starting Thread: " + thread1.IsAlive);
thread1.Abort();
Console.WriteLine("After Aborting Thread: " + thread1.IsAlive);
Console.ReadLine();
}
}
}
0 comments:
Post a Comment