﻿using System;
using System.Threading;

namespace ConsoleTestStuff.Threading
{
    public class ThreadTest : IThreadTest
    {
        public void Run()
        {
            Thread t = new Thread(new ThreadStart(ThreadTestMethods.LongTimeMethod));
            t.Start();

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Main thread do some work ...");
                Thread.Sleep(0);
            }

            t.Join();
        }
    }

    public class BackgroundThreadTest : IThreadTest
    {
        public void Run()
        {
            Thread t = new Thread(new ThreadStart(ThreadTestMethods.LongTimeMethod));
            t.IsBackground = true;
            t.Start();
        }
    }

    public class ParameterizedThreadTest : IThreadTest
    {
        public void Run()
        {
            Thread t = new Thread(new ParameterizedThreadStart(ThreadTestMethods.ParamerizedMethod));
            t.Start(5);
            t.Join();
        }
    }

    public class AbortThreadTest : IThreadTest
    {
        public void Run()
        {
            bool stopped = false;

            Thread t = new Thread(new ThreadStart(() =>
                {
                    while(!stopped)
                    {
                        Console.WriteLine("Running ...");
                        Thread.Sleep(1000);
                    }
                }));

            t.Start();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            stopped = true;
            t.Join();
        }
    }
}
