From 32d95221e6c94420568ff79ab3ec07ffb4c58032 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 7 Oct 2026 18:36:59 +0200 Subject: [PATCH] Added: initial multithreading solution with projects for threads and tasks --- Multithreading/.gitignore | 133 ++++++++++++++++++ .../.idea.Multithreading/.idea/.gitignore | 15 ++ .../.idea/indexLayout.xml | 8 ++ .../.idea/.idea.Multithreading/.idea/vcs.xml | 6 + Multithreading/Multithreading.sln | 22 +++ Multithreading/Tasks/Program.cs | 72 ++++++++++ Multithreading/Tasks/Tasks.csproj | 10 ++ Multithreading/Threads/Program.cs | 110 +++++++++++++++ Multithreading/Threads/Threads.csproj | 10 ++ 9 files changed, 386 insertions(+) create mode 100755 Multithreading/.gitignore create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/.gitignore create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml create mode 100644 Multithreading/Multithreading.sln create mode 100644 Multithreading/Tasks/Program.cs create mode 100644 Multithreading/Tasks/Tasks.csproj create mode 100644 Multithreading/Threads/Program.cs create mode 100644 Multithreading/Threads/Threads.csproj diff --git a/Multithreading/.gitignore b/Multithreading/.gitignore new file mode 100755 index 0000000..8c7f0e8 --- /dev/null +++ b/Multithreading/.gitignore @@ -0,0 +1,133 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.svclog +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.azurePubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +packages/ +## TODO: If the tool you use requires repositories.config, also uncomment the next line +!packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +![Ss]tyle[Cc]op.targets +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml + +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +# ========================= +# Windows detritus +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac desktop service store files +.DS_Store + +_NCrunch* diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore b/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore new file mode 100644 index 0000000..468ee63 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore @@ -0,0 +1,15 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.Multithreading.iml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml b/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml b/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Multithreading/Multithreading.sln b/Multithreading/Multithreading.sln new file mode 100644 index 0000000..eecf0ff --- /dev/null +++ b/Multithreading/Multithreading.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Threads", "Threads\Threads.csproj", "{4FDD302A-C89D-4853-A09B-F96D63977A1A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tasks", "Tasks\Tasks.csproj", "{BBDCC3D1-235F-407F-B0FC-E6F48F6D16F3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4FDD302A-C89D-4853-A09B-F96D63977A1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4FDD302A-C89D-4853-A09B-F96D63977A1A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4FDD302A-C89D-4853-A09B-F96D63977A1A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4FDD302A-C89D-4853-A09B-F96D63977A1A}.Release|Any CPU.Build.0 = Release|Any CPU + {BBDCC3D1-235F-407F-B0FC-E6F48F6D16F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBDCC3D1-235F-407F-B0FC-E6F48F6D16F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBDCC3D1-235F-407F-B0FC-E6F48F6D16F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBDCC3D1-235F-407F-B0FC-E6F48F6D16F3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Multithreading/Tasks/Program.cs b/Multithreading/Tasks/Program.cs new file mode 100644 index 0000000..741cd11 --- /dev/null +++ b/Multithreading/Tasks/Program.cs @@ -0,0 +1,72 @@ +Console.WriteLine("----- Program started -----"); + +// Task task = new Task(() => +// { +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); +// +// for (int i = 0; i < 10; i++) +// { +// Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// Thread.Sleep(500); +// } +// +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); +// } +// ); +// task.Start(); +// +// Task task2 = Task.Run(() => +// { +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); +// +// for (int i = 0; i < 10; i++) +// { +// Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// Thread.Sleep(500); +// } +// +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); +// } +// ); +// +// task.Wait(); +// task2.Wait(); +// +// Task task3 = new Task(() => +// { +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); +// +// for (int i = 0; i < 10; i++) +// { +// Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// Thread.Sleep(500); +// } +// +// Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); +// } +// ); +// task3.RunSynchronously(); + +Task[] tasks = new Task[10]; +Random random = new Random(); +for (int i = 0; i < tasks.Length; i++) +{ + tasks[i] = Task.Run(() => + { + Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); + + for (int i = 0; i < 10; i++) + { + Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); + Thread.Sleep(random.Next(100, 1000)); + } + + Console.WriteLine($"Task with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); + } + ); +} + +// Task.WaitAll(tasks); +Task.WaitAny(tasks); + +Console.WriteLine("----- Program ended -----"); \ No newline at end of file diff --git a/Multithreading/Tasks/Tasks.csproj b/Multithreading/Tasks/Tasks.csproj new file mode 100644 index 0000000..6c1dc92 --- /dev/null +++ b/Multithreading/Tasks/Tasks.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/Multithreading/Threads/Program.cs b/Multithreading/Threads/Program.cs new file mode 100644 index 0000000..cf23e4b --- /dev/null +++ b/Multithreading/Threads/Program.cs @@ -0,0 +1,110 @@ +Console.WriteLine("----- Program started -----"); + +Console.WriteLine($"ThreadId: {Thread.CurrentThread.ManagedThreadId}"); + +// Console.WriteLine("Before Sleep"); +// Thread.Sleep(5000); +// Console.WriteLine("After Sleep"); + +#region Single Threads +// Thread thread = new Thread(() => +// { +// Console.WriteLine("Thread started"); +// Console.WriteLine($"ThreadId: {Thread.CurrentThread.ManagedThreadId}"); +// +// // Thread.Sleep(5000); +// +// for (int i = 0; i < 5; i++) +// { +// Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// Thread.Sleep(500); +// } +// +// Console.WriteLine("Thread stopped"); +// }); +// thread.IsBackground = true; +// thread.Start(); +// +// Thread[] threads = new Thread[10]; +// for (int i = 0; i < threads.Length; i++) +// { +// threads[i] = new Thread(PrintAndSleep); +// threads[i].IsBackground = true; +// threads[i].Start(); +// } +// +// Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state before join: {thread.ThreadState}"); +// thread.Join(); // waits for the thread to be finished +// Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state after join: {thread.ThreadState}"); +// +// // thread.Start(); // CAUTION: an already started or terminated thread cannot be started again +// +// while (threads[0].ThreadState != ThreadState.Stopped) +// { +// Console.WriteLine($"Thread ({threads[0].ManagedThreadId}) current state: {threads[0].ThreadState}"); +// threads[0].Join(50); +// +// // update the ui +// } +// +// for (int i = 0; i < threads.Length; i++) +// { +// Console.WriteLine($"Thread ({threads[i].ManagedThreadId}) current state before join: {threads[i].ThreadState}"); +// threads[i].Join(1000); // waits for the thread to be finished +// Console.WriteLine($"Thread ({threads[i].ManagedThreadId}) current state after join: {threads[i].ThreadState}"); +// } +// +// for (int i = 0; i < threads.Length; i++) +// { +// threads[i] = new Thread(PrintAndSleep); +// threads[i].Start(); +// } +#endregion + +Console.WriteLine("----- THREADPOOL -----"); + +Console.WriteLine($"SetMaxThread to 1: {ThreadPool.SetMaxThreads(1, 1)}"); +Console.WriteLine($"SetMaxThread to 5: {ThreadPool.SetMaxThreads(5, 5)}"); +Console.WriteLine($"SetMaxThread to 20: {ThreadPool.SetMaxThreads(20, 20)}"); + +for (int i = 0; i < 10; i++) +{ + ThreadPool.QueueUserWorkItem(_ => PrintAndSleep()); + // ThreadPool.QueueUserWorkItem(PrintAndSleepWithObject); +} + +Thread.Sleep(5000); + +for (int i = 0; i < 10; i++) +{ + ThreadPool.QueueUserWorkItem(PrintAndSleepWithObject); +} + +Console.WriteLine("----- Program finished -----"); + + +static void PrintAndSleep() +{ + Console.WriteLine($"Thread with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); + + for (int i = 0; i < 10; i++) + { + Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); + Thread.Sleep(500); + } + + Console.WriteLine($"Thread with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); +} + +static void PrintAndSleepWithObject(object? obj) +{ + Console.WriteLine($"Thread with ThreadId {Thread.CurrentThread.ManagedThreadId} started"); + + for (int i = 0; i < 10; i++) + { + Console.WriteLine($"ThreadId ({Thread.CurrentThread.ManagedThreadId}): {i}"); + Thread.Sleep(500); + } + + Console.WriteLine($"Thread with ThreadId {Thread.CurrentThread.ManagedThreadId} stopped"); +} \ No newline at end of file diff --git a/Multithreading/Threads/Threads.csproj b/Multithreading/Threads/Threads.csproj new file mode 100644 index 0000000..6c1dc92 --- /dev/null +++ b/Multithreading/Threads/Threads.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + +