Thread.Sleep() 을 사용하면 일정 시간 동안 Thread를 멈출 수 있다. TimeSpan.FromSeconds() 을 사용하면 편하게 milli second 계산을 할 수 있다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { static object locker = new object(); static void Main(string[] args) { new Thread(Go).Start(); for (int i = 0; i < 20; i++) { Console.Write("."); Thread.Sleep(TimeSpan.FromSeconds(1)); } } static void ..
thread의 실행을 컨트롤하기 위해서 locker를 사용할 수 있다. locker는 오로지 lock을 하는 용도로만 쓰인다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { static object locker = new object(); static void Main(string[] args) { new Thread(Go).Start(); lock (locker) { for (int i = 0; i < 500; i++) { Console.Write("."); } } } static void Go() { lock (locker) { for (int i = 0; i < 500; i++) { C..
static field는 모든 Thread에서 공동으로 사용된다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { // Static fields are shared between all threads static int count; static void Main(string[] args) { new Thread(Go).Start(); Go(); } static void Go() { for (int i = 0; i < 5; i++) { count++; Console.WriteLine(count); } } } } 위의 예제에서 count는 static field이다. 두개의 thread가 coun..
아래는 두개의 Thread가 공동의 instance를 사용한 예제이다. Foo 클래스 인스턴스인 foo를 공동으로 사용한 두개의 thread가 실행되었다. using System; using System.Threading; namespace ThreadTest { class Program { static void Main(string[] args) { Foo foo = new Foo(); new Thread(foo.Go).Start(); foo.Go(); } } class Foo { private int count = 0; public void Go() { for (int i = 0; i < 5; i++) { count++; Console.WriteLine(count); } } } } 실행 결과 화면은 ..
아래 예제는 Thread를 간단하게 시작하는 방법을 사용한 것이다. 단 한줄로 Thread를 시작한다. using System; using System.Threading; namespace ThreadTest { class Program { static void Main(string[] args) { new Thread(Go).Start(); // Call Go() on a new thread Go(); // Call Go() on the main thread } static void Go() { for (int i = 0; i < 5; i++) Console.WriteLine(i); } } } 아래는 실행 결과 화면이다.
http://www.albahari.com/threading/ Threading in C# 은 위의 사이트에서 PDF 로 다운로드 할 수도 있다. 하지만 그냥 웹 상에서 보는 것을 권장한다. 왜냐하면 웹 상의 문서들은 종종 업데이트가 되기 때문이다. Joseph Albahari 는 C# 3.0 in a Nutshell 의 저자이다. 이 사람이 쓴 책들이 몇 권 있는데, http://www.albahari.com 이곳에 가면 확인할 수 있다.
하나의 Thread가 실행되는 도중, 다른 Thread가 완료되기를 기다려야할 때가 있다. 이때 사용할 수 있는 것이 Join method이다. Join method는 지정된 Thread가 끝날 때까지 기다리라는 명령어이다. 아래의 예제를 보자. using System; using System.Threading; namespace JoinTest { class Program { static void Main(string[] args) { Thread worker = new Thread(new ThreadStart(DoWork)); worker.Start(); worker.Join(); Console.WriteLine("Done"); } static void DoWork() { for (int i = 0;..
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 Start..
앞 예제에서는 Monitor 클래스를 사용해서 object를 lock 시켰다. 이번에는 lock keyword를 사용해보자. lock keyword를 사용하면 Monitor를 사용하는 것보다 안전하고 간편하게 thread를 사용할 수 있다. 아래 예제를 보자. 예제에서 보면 Monitor.Enter 와 Monitor.Exit 대신 lock 블럭으로 감싸준 것을 알 수 있다. using System; using System.Collections.Generic; using System.Threading; namespace ThreadSample { class Program { private static Queue myQueue = new Queue(); static void Main(string[] args..
Monitor.Enter 와 Monitor.Exit 샘플코드이다. Monitor.Enter 로 필요한 object를 lock 시킨다. Monitor.Enter 로 lock 시킨 object 는 Monitor.Exit로 풀어준다. 아래 코드에서 주의할 것이 하나 있는데, 바로 foreach 문이다. foreach 문이 완료되기 전에 대상 object 의 내용이 변해서는 안되다. 그래서 foreach 문은 그 앞뒤에 Monitor.Enter 와 Monitor.Exit 를 넣었다. using System; using System.Collections.Generic; using System.Threading; namespace ThreadSample { class Program { private static Q..
- Total
- Today
- Yesterday
- Microsoft
- iTextSharp
- AxWebBrowser
- Service pack
- autohotkey
- 유틸리티
- tagREADYSTATE
- AdSense감추기
- .net framework
- download.com
- WinAutomation
- 스크린캡쳐
- iText
- READYSTATE_COMPLETE
- Rollback Rx
- 애드센스감추기
- ScreenHunter
- registry
- Sample Code
- windows
- AdSense숨기기
- c#
- Regular Expressions
- Automation
- jre
- java
- 애드센스숨기기
- Phalanger
- DotNetMagic
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |