public static void DoWork(object state) { Stopper stopper = (Stopper)state; for (int i = 0; i < 100000; ++i) { if (stopper.ShouldStop) { Thread.CurrentThread.Abort(); } Console.WriteLine(i); } }
static void Main(string[] args) { var stopper = new Stopper(); ThreadPool.QueueUserWorkItem(DoWork, stopper); Thread.Sleep(2000); stopper.ShouldStop = true; Console.ReadLine(); var cancellation = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem(DoWork2, cancellation.Token); Thread.Sleep(2000); cancellation.Cancel(); Console.ReadLine(); }