Category (302) 썸네일형 리스트형 Thread 간단하게 시작하기 예제 아래 예제는 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); } } } 아래는 실행 결과 화면이다. 웹 사이트 : Threading in C# by Joseph Albahari http://www.albahari.com/threading/ Threading in C# 은 위의 사이트에서 PDF 로 다운로드 할 수도 있다. 하지만 그냥 웹 상에서 보는 것을 권장한다. 왜냐하면 웹 상의 문서들은 종종 업데이트가 되기 때문이다. Joseph Albahari 는 C# 3.0 in a Nutshell 의 저자이다. 이 사람이 쓴 책들이 몇 권 있는데, http://www.albahari.com 이곳에 가면 확인할 수 있다. Thread Join 사용법과 예제 하나의 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 사용법과 예제 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.. Thread & Lock 사용법과 예제 앞 예제에서는 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.. Thread & Monitor 사용 방법과 예제 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.. [C#] C# 2.0 스타일 Thread 사용법과 예제 C# 2.0 에서는 ThreadStart delegate를 사용하지 않아도 된다. 아래 예제를 보자. 아래 예제는 앞 예제와 같은 것이다. 하지만 ThreadStart delegate를 사용하지 않았다. using System; using System.Threading; namespace ThreadSample { class Program { public const int Repetitions = 1000; static void Main(string[] args) { Thread myThread = new Thread(DoWork); myThread.Start(); for (int count = 0; count < Repetitions; count++) { Console.Write('-'); } myTh.. [C#] Thread 샘플 코드 4 Thread 간에 서로 얼마자 자주 interupt 가 일어나는지를 알 수 있는 샘플 코드이다. 두개의 thread가 화면에 점과 대쉬를 표시한다. 결과 화면을 보면 인터럽트의 정도를 확인할 수 있다. using System; using System.Threading; namespace ThreadSample { class Program { public const int Repetitions = 1000; static void Main(string[] args) { ThreadStart myThreadStart = new ThreadStart(DoWork); Thread myThread = new Thread(myThreadStart); myThread.Start(); for (int count = 0;.. 이전 1 ··· 26 27 28 29 30 31 32 ··· 38 다음