이전 .NET 버전에서는 Thread.Suspend() 메소드를 통해서 Thread를 외부에서 강제로 종료 시킬 수 있었다.
이 메소드는 현재 deprecate되어 다음 아래와 같은 방식으로 메소드를 강제 종료 할 수 있다.
실행 결과는 다음과 같다. 실행해 보면 Thread 내부의 상태와 상관없이 Thread.Interrupt() 메소드 호출과 동시에
Thread 내부에서 ThreadInterruptedException 이 발생하며, 이를 처리하면 곧 바로 Thread를 강제로 종료시킬 수 있다
이 메소드는 현재 deprecate되어 다음 아래와 같은 방식으로 메소드를 강제 종료 할 수 있다.
using System;
using System.Threading;
namespace ThreadTest
{
internal class Program
{
private static void ThreadProc()
{
try
{
Thread.Sleep(10000);
}
catch(ThreadInterruptedException)
{
Console.WriteLine("Thread:Interrunpted");
return;
}
}
private static void Main()
{
Console.WriteLine(".NET Ver: " +Environment.Version);
Thread t = new Thread(ThreadProc);
t.Start();
Thread.Sleep(2000);
Console.WriteLine("Force to terminate the thread");
t.Interrupt();
t.Join();
Console.WriteLine("Application Ended");
}
}
}실행 결과는 다음과 같다. 실행해 보면 Thread 내부의 상태와 상관없이 Thread.Interrupt() 메소드 호출과 동시에
Thread 내부에서 ThreadInterruptedException 이 발생하며, 이를 처리하면 곧 바로 Thread를 강제로 종료시킬 수 있다



최근 덧글