using System;
using System.Threading;
using System.Reflection;
// csc thread_IsAilve_Stop_2.cs
namespace ex_threading
{
public class thread_state
{
public thread_state() { Console.WriteLine("Thread Constructor ");
Thread.Sleep(3000); }
private volatile bool _stopnow;
public void process() {
Console.WriteLine(Thread.CurrentThread.Name.ToString());
int n = 0;
do{
p_string("hello");
p_number(25);
n++;}while(n < 1 );
if(!_stopnow){approve();}
}
public void approve() { Console.WriteLine(" Job is done with Grace ");_stopnow
= true ; Console.WriteLine("Please Hit Enter ");
}
public static void p_string(string str1) {
Console.WriteLine(Thread.CurrentThread.Name.ToString()+ " -string-");
string str = str1;
Thread.Sleep(1000);
Console.WriteLine(": string " + str + " processed at: "+ DateTime.Now);}
private static void p_number(int n3) {
int ni = n3;
Console.WriteLine(Thread.CurrentThread.Name.ToString()+ " -number-");
Thread.Sleep(1000);
Console.WriteLine(": Number " + ni + " processed at : "+ DateTime.Now);}
}//end of class thread_state
class test
{
static void Main()
{
DateTime dt = DateTime.Now;
Console.WriteLine("Object is being created {0} ", dt);
thread_state ts = new thread_state();
Console.WriteLine("Back to Main");
DateTime dt2 = DateTime.Now;
Console.WriteLine("Time lapsed {0} ", dt2);
Thread str = new Thread(ts.process);
str.Name = "Job Handler";
//p_string("process string" );
str.Start();
string str_killed = str.Name ;
if(str.IsAlive){ Console.WriteLine("Thread " + str_killed + " is Alive");
}
Thread.Sleep(1);
Console.WriteLine("-----");
Console.ReadLine();
str.Abort();
if(str.IsAlive){ Console.WriteLine("Thread " + str_killed + " is still
alive"); }
else { Console.WriteLine("Thread " + str_killed + " is dead");}
Console.ReadLine();
}
}//endo of class test
}//end of namespace