■ Thread 클래스에서 클래스 객체를 사용해 인자를 전달하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
using System.Threading; ThreadSample threadSample = new ThreadSample(10); Thread thread = new Thread(new ThreadStart(threadSample.DisplayCount)); thread.Name = "thread"; thread.Start(); |
▶ ThreadSample.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Threading; /// <summary> /// 스레드 샘플 /// </summary> public class ThreadSample { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 루프 카운트 /// </summary> private readonly int loopCount; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ThreadSample(loopCount) /// <summary> /// 생성자 /// </summary> /// <param name="loopCount">루프 카운트</param> public ThreadSample(int loopCount) { this.loopCount = loopCount; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 카운트 표시하기 - DisplayCount() /// <summary> /// 카운트 표시하기 /// </summary> public void DisplayCount() { for(int i = 1; i <= loopCount; i++) { Thread.Sleep(TimeSpan.FromSeconds(0.5)); Console.WriteLine("{0} : 카운트 {1}", Thread.CurrentThread.Name, i); } } #endregion } |