using System;
using System.Threading;
namespace ConsoleApplication_C_Sharp
{
public class myClass
{
public void Method()
{
while (true)
{
Console.WriteLine("myClass Method - run in thread");
}
}
}
class Program
{
static void Main(string[] args)
{
myClass myclass = new myClass();
Thread thread = new Thread(new ThreadStart(myclass.Method));
thread.Start();
// waiting for the thread to start
while (!thread.IsAlive) ;
Thread.Sleep(2);
thread.Abort();
Console.WriteLine();
Console.WriteLine("Thread finished");
try
{
thread.Start();
}
catch (ThreadStateException ex)
{
Console.Write("Exception: {0}", ex.Message);
}
}
}
}
/*
run:
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
myClass Method - run in thread
...
Thread finished
Exception: Thread is running or terminated; it cannot restart.
*/