반응형
주로 스레드에서 사용하는 함수는 다음과 같다
[ 스레드 시작 ]
Start() : 스레드를 시작한다.
[ 스레드 중지 ]
Abort() : 스레드를 중지하는데 강제종료시킨다. 어디서 종료할지 모른다
Join() : 스레드를 중지하는데 다 실행될 때 까지 기다렸다 종료한다.
[ 스레드 잠시 멈춤 ]
Sleep(시간) : Sleep 모드로 들어가게 된다. 파라미터로 시간을 넘기는데
넘긴 시간만큼 멈추었다가 다시 진행된다.
Suspend() : Resume() 명령을 실행하기 전까지 멈추게 된다.
스레드의 상태값을 알아올때는 ThreadState property 를 사용하는데
그림을 참고하면 쉽게 이해가 갈것이다.
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample
{
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main()
{
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}반응형
'기타 언어 > C# & MFC' 카테고리의 다른 글
| C# 파일검색 간단한 예제 (1) | 2009.04.18 |
|---|---|
| 탐색기에 나와있는 간단한 TreeView, ListView 구현예제 (0) | 2009.04.14 |
| 탐색기에 나와있는 간단한 TreeView 구현예제 (0) | 2009.04.12 |
| C# 에서 ActiveX Control 간단하게 만들어보기 (0) | 2009.04.07 |
| DB 검색 데이타 양이 많아 메인 화면이 멈추는걸 막고 독립적인 Thread 로 돌리고 싶을때 (2) | 2009.03.21 |
| List 나 Grid 등에 대량의 데이터를 업로드 할 때 화면멈춤 해결하기 (0) | 2009.03.19 |
| C# 에서 Thread Safe 의 2가지 사용법 (0) | 2009.03.19 |
| delegate 간단하게 사용설명과 예제를 보여준다. (2) | 2009.03.18 |
