C#

[이것이 C#이다] Thread abort, start, join

인생개발 이정환 2024. 9. 21. 17:47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SideTask task = new SideTask(100);
            Thread t1 = new Thread(new ThreadStart(task.KeepAlive));
            t1.IsBackground = false;

            Console.WriteLine("Starting thread");
            t1.Start();

            Thread.Sleep(100);

            Console.WriteLine("Aborting thread..");
            t1.Abort();

            Console.WriteLine("Waiting until thread stops..");
            t1.Join();
        }

        class SideTask
        {
            int count;
            public SideTask(int count)

            {
                this.count = count;
            }

            public void KeepAlive()
            {
                try
                {
                    while (count > 0)
                    {
                        Console.WriteLine($"{count--}left");
                        Thread.Sleep(10);

                    }
                    Console.WriteLine("Count : 0");\

                }
                catch(ThreadAbortException e)  
                {
                    Console.WriteLine(e);
                    Thread.ResetAbort();
                }
                finally
                {
                    Console.WriteLine("Clearing resource...");
                }

            }
        }
    }
}