C#/수업내용

03/29 Thread 복습 2 (Abort)

박준희 2021. 3. 29. 22:53
728x90

 

 

Abort()메서드를 사용하는것으로 ThreadAbortException를 발생시켜 스레드 종료 프로세스를 시작

 

Program.cs

namespace Study07
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

 

App.cs

using System;
using System.Threading;

namespace Study07
{
    public class App
    {
        public App()
        {
            Console.WriteLine("App");
            Thread t = new Thread(new ThreadStart(this.TestThread));
            t.Start();

            Thread.Sleep(3000);
            t.Abort("Information from Main.");
        }
        
        private void TestThread()
        {
            try
            {
                while(true)
                {
                    Console.WriteLine("Thread is running...");
                    Thread.Sleep(1000);
                }
            }
            catch (ThreadAbortException e)
            {
                Console.WriteLine((string)e.ExceptionState);
            }
        }
    }
}
728x90