From 36dc8793f2bad42196ed790220c832635bb8ff30 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 8 Nov 2022 10:56:46 +0900 Subject: [PATCH] create thread --- MultiThreadingStudy/AboutThread/App.config | 6 + .../AboutThread/CreateThread.cs | 116 ++++++++++++++++++ .../AboutThread/CreateThread.csproj | 54 ++++++++ MultiThreadingStudy/AboutThread/Program.cs | 28 +++++ .../AboutThread/Properties/AssemblyInfo.cs | 36 ++++++ MultiThreadingStudy/MultiThreadingStudy.sln | 25 ++++ 6 files changed, 265 insertions(+) create mode 100644 MultiThreadingStudy/AboutThread/App.config create mode 100644 MultiThreadingStudy/AboutThread/CreateThread.cs create mode 100644 MultiThreadingStudy/AboutThread/CreateThread.csproj create mode 100644 MultiThreadingStudy/AboutThread/Program.cs create mode 100644 MultiThreadingStudy/AboutThread/Properties/AssemblyInfo.cs create mode 100644 MultiThreadingStudy/MultiThreadingStudy.sln diff --git a/MultiThreadingStudy/AboutThread/App.config b/MultiThreadingStudy/AboutThread/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/MultiThreadingStudy/AboutThread/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MultiThreadingStudy/AboutThread/CreateThread.cs b/MultiThreadingStudy/AboutThread/CreateThread.cs new file mode 100644 index 0000000..c6bf001 --- /dev/null +++ b/MultiThreadingStudy/AboutThread/CreateThread.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace CreateThread +{ + internal class CreateThread + { + public static void Create() + { + // Do in new thread + Thread t1 = new Thread(new ThreadStart(Run)); + t1.Start(); + + // Do in main thread + Run(); + } + + public static void CreateManyWay() + { + // Create ThreadStart delegate + Thread t1 = new Thread(new ThreadStart(Run)); + t1.Start(); + + // Infer by method and create delgate + Thread t2 = new Thread(Run); + t2.Start(); + + // Use anonymous method + Thread t3 = new Thread(delegate () + { + Run(); + }); + t3.Start(); + + // Use lambda expression + Thread t4 = new Thread(() => Run()); + t4.Start(); + + // Short + new Thread(() => Run()).Start(); + } + + public static void CreateParameterized() + { + Thread t1 = new Thread(new ParameterizedThreadStart(Calc)); + t1.Start(10.0); + + Thread t2 = new Thread(() => Sum(10, 20, 30)); + t2.Start(); + } + + public static void CreateOtherClassMethod() + { + OuterClass obj = new OuterClass(); + Thread t = new Thread(obj.Run); + t.Start(); + } + + public static void CreateBackgroundThread() + { + Thread t = new Thread(new ThreadStart(EndlessRun)); + t.IsBackground = true; + t.Start(); + } + + public static void CreateForegroundThread() + { + Thread t = new Thread(new ThreadStart(EndlessRun)); + t.IsBackground = false; // default + t.Start(); + } + + private static void Run() + { + Console.WriteLine($"Thread#{Thread.CurrentThread.ManagedThreadId}: Start"); + // Do something + Thread.Sleep(3000); + Console.WriteLine($"Thread#{Thread.CurrentThread.ManagedThreadId}: Finish"); + } + + private static void Calc(object radius) + { + double r = (double)radius; + double area = r * r * Math.PI; + Console.WriteLine($"r={radius}, area={area}"); + } + + private static void Sum(int d1, int d2, int d3) + { + int sum = d1 + d2 + d3; + Console.WriteLine($"sum={sum}"); + } + + private static void EndlessRun() + { + int i = 0; + while (true) + { + Console.WriteLine($"running... ({++i})"); + Thread.Sleep(1000); + } + } + } + + internal class OuterClass + { + public void Run() + { + Console.WriteLine("OuterClass.Run"); + } + } +} diff --git a/MultiThreadingStudy/AboutThread/CreateThread.csproj b/MultiThreadingStudy/AboutThread/CreateThread.csproj new file mode 100644 index 0000000..4158db6 --- /dev/null +++ b/MultiThreadingStudy/AboutThread/CreateThread.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {8724C07D-B729-4CEC-AA91-BEEA9C96409C} + Exe + AboutThread + AboutThread + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MultiThreadingStudy/AboutThread/Program.cs b/MultiThreadingStudy/AboutThread/Program.cs new file mode 100644 index 0000000..202f117 --- /dev/null +++ b/MultiThreadingStudy/AboutThread/Program.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CreateThread +{ + internal class Program + { + static void Main(string[] args) + { + //CreateThread.Create(); + + //CreateThread.CreateManyWay(); + + //CreateThread.CreateOtherClassMethod(); + + //CreateThread.CreateParameterized(); + + //CreateThread.CreateBackgroundThread(); + + CreateThread.CreateForegroundThread(); + + //Console.ReadKey(); + } + } +} diff --git a/MultiThreadingStudy/AboutThread/Properties/AssemblyInfo.cs b/MultiThreadingStudy/AboutThread/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9294e26 --- /dev/null +++ b/MultiThreadingStudy/AboutThread/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("AboutThread")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("AboutThread")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("8724c07d-b729-4cec-aa91-beea9c96409c")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MultiThreadingStudy/MultiThreadingStudy.sln b/MultiThreadingStudy/MultiThreadingStudy.sln new file mode 100644 index 0000000..12adebb --- /dev/null +++ b/MultiThreadingStudy/MultiThreadingStudy.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateThread", "AboutThread\CreateThread.csproj", "{8724C07D-B729-4CEC-AA91-BEEA9C96409C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8724C07D-B729-4CEC-AA91-BEEA9C96409C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8724C07D-B729-4CEC-AA91-BEEA9C96409C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8724C07D-B729-4CEC-AA91-BEEA9C96409C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8724C07D-B729-4CEC-AA91-BEEA9C96409C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {ABBF91D6-78C7-423A-9442-2C968B7061DA} + EndGlobalSection +EndGlobal