Thread 정리
- 일반 사용
- new Thread(Work);
- ThreadStart 사용
- new Thread(new ThreadStart(Work));
- 람다식 사용
- new Thread(() => Work());
- ParameterizedThreadStart 사용
- new Thread(new ParameterizedThreadStart(WorkWithParam));
- 람다식 이용하여 Return Value 받기 (returnValue는 선언한 object 변수)
- new Thread(() => { returnValue = ReturnableWork(); });
Start a thread
Start a thread with parameter
Start a thread with return value
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 | public static class ThreadExam { public static void ThreadTest() { Thread thread; // Start a thread new Thread(Work).Start(); new Thread(() => Work()).Start(); thread = new Thread(new ThreadStart(Work)); thread.Start(); thread.Join(); // Start a thread with parameter thread = new Thread(new ParameterizedThreadStart(WorkWithParam)); thread.Start("Parameters"); // Start a thread with return value object returnValue = null; thread = new Thread(() => { returnValue = ReturnableWork(); }); thread.Start(); thread.Join(); Console.WriteLine(returnValue); } public static void Work() { Console.WriteLine($"Execute Work() : ThreadId:{Thread.CurrentThread.ManagedThreadId}"); } public static void WorkWithParam(object obj) { Console.WriteLine($"Execute Work(object) : object = {obj} : ThreadId:{Thread.CurrentThread.ManagedThreadId}"); } public static object ReturnableWork() { Console.WriteLine($"Execute ReturnableWork() : ThreadId:{Thread.CurrentThread.ManagedThreadId}"); return "ReturnValue"; } } | cs |
'worklog > C#' 카테고리의 다른 글
[C#/Devexpress/Winform] GridControl의 아이템을 Drag & Drop 하여 다른 Control에 전달하는 방법 소개 (0) | 2021.08.21 |
---|---|
C# 버전 별 추가된 기능 (0) | 2018.08.19 |
Call by Reference (param, ref, out) (0) | 2018.06.30 |
Environment 클래스 (0) | 2018.06.30 |
What the C or C++ Programmer Needs to Know About C# and the .NET Framework (0) | 2018.06.30 |