티스토리 뷰
Thread를 중지시키기 위해서 Abort method를 사용할 수 있다.
Abort method 로 thread 를 중지시키면 ThreadAbortException 이 발생한다.
아래 예제는 ThreadAbortException 을 발생시키는 예제이다.
thread 가 실행되는 도중 enter 키를 누르면, thread 가 정지되고, ThreadAbortException 이 발생한다.
using System;
using System.Threading;
namespace AbortThreads
{
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "MAIN";
PrintMessage("Application Started.");
Thread worker = new Thread(new ThreadStart(DoWork));
worker.Name = "WORKER";
worker.Start();
Console.WriteLine("Press Enter to Abort the Thread!");
Console.ReadLine();
worker.Abort();
Console.WriteLine("Thread abort signal sent.");
Console.ReadLine();
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", Thread.CurrentThread.Name, msg);
}
static void DoWork()
{
try
{
while (true)
{
Console.Write("...");
Thread.Sleep(100);
}
}
catch (Exception e)
{
PrintMessage("Trapped:" + e.ToString());
}
}
}
}
아래는 실행 화면이다.
Abort method 로 thread 를 중지시키면 ThreadAbortException 이 발생한다.
아래 예제는 ThreadAbortException 을 발생시키는 예제이다.
thread 가 실행되는 도중 enter 키를 누르면, thread 가 정지되고, ThreadAbortException 이 발생한다.
using System;
using System.Threading;
namespace AbortThreads
{
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "MAIN";
PrintMessage("Application Started.");
Thread worker = new Thread(new ThreadStart(DoWork));
worker.Name = "WORKER";
worker.Start();
Console.WriteLine("Press Enter to Abort the Thread!");
Console.ReadLine();
worker.Abort();
Console.WriteLine("Thread abort signal sent.");
Console.ReadLine();
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", Thread.CurrentThread.Name, msg);
}
static void DoWork()
{
try
{
while (true)
{
Console.Write("...");
Thread.Sleep(100);
}
}
catch (Exception e)
{
PrintMessage("Trapped:" + e.ToString());
}
}
}
}
아래는 실행 화면이다.
'Code > C#' 카테고리의 다른 글
Thread 간단하게 시작하기 예제 (0) | 2007.12.09 |
---|---|
Thread Join 사용법과 예제 (0) | 2007.12.08 |
Thread & Lock 사용법과 예제 (0) | 2007.12.08 |
Thread & Monitor 사용 방법과 예제 (0) | 2007.12.08 |
[C#] C# 2.0 스타일 Thread 사용법과 예제 (0) | 2007.12.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- .net framework
- Microsoft
- registry
- WinAutomation
- 애드센스숨기기
- ScreenHunter
- AdSense감추기
- iTextSharp
- jre
- Phalanger
- AxWebBrowser
- 스크린캡쳐
- Automation
- iText
- c#
- Regular Expressions
- Service pack
- 유틸리티
- tagREADYSTATE
- windows
- download.com
- java
- autohotkey
- Sample Code
- 애드센스감추기
- DotNetMagic
- READYSTATE_COMPLETE
- AdSense숨기기
- Rollback Rx
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함