Added Multithreading project with examples and solution configuration

This commit is contained in:
2026-07-06 18:41:35 +02:00
parent 0512201ce0
commit df39245429
9 changed files with 376 additions and 0 deletions
+133
View File
@@ -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*
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/projectSettingsUpdater.xml
/contentModel.xml
/.idea.Multithreading.iml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
+10
View File
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+16
View File
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading", "Multithreading.csproj", "{D89AC5D5-FFB2-453F-A908-0C69383D976E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D89AC5D5-FFB2-453F-A908-0C69383D976E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D89AC5D5-FFB2-453F-A908-0C69383D976E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D89AC5D5-FFB2-453F-A908-0C69383D976E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D89AC5D5-FFB2-453F-A908-0C69383D976E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
+83
View File
@@ -0,0 +1,83 @@
using System;
using System.Threading.Tasks;
namespace Multithreading
{
internal static class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Program started");
// Task task = new Task(() =>
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} started");
// for (int i = 0; i < 10; i++)
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId}: {i}");
// Thread.Sleep(500);
// }
//
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} stopped");
// });
// task.Start();
//
//
// Task task2 = Task.Run(() =>
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} started");
// for (int i = 0; i < 10; i++)
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId}: {i}");
// Thread.Sleep(500);
// }
//
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} stopped");
// });
//
// task.Wait();
// task2.Wait();
//
// Task task3 = new Task(() =>
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} started");
// for (int i = 0; i < 10; i++)
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId}: {i}");
// Thread.Sleep(500);
// }
//
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} stopped");
// });
// task3.RunSynchronously();
// Task[] tasks = new Task[10];
// Random random = new Random();
// for (int i = 0; i < 10; i++)
// {
// tasks[i] = Task.Run(() =>
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} started");
// for (int j = 0; j < 10; j++)
// {
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId}: {j}");
// Thread.Sleep(random.Next(100, 1000));
// }
//
// Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId} stopped");
// });
// }
//
// // Task.WaitAll(tasks);
// Task.WaitAny(tasks);
Parallel.For(0, 10000, i =>
{
// do whatever you want to do in parallel
Console.WriteLine($"Task with Thread {Thread.CurrentThread.ManagedThreadId}: {i}");
});
Console.WriteLine("Program ended");
}
}
}
+108
View File
@@ -0,0 +1,108 @@
// using System;
// using System.Threading;
//
// namespace Multithreading
// {
// internal static class Program
// {
// public static void Main(string[] args)
// {
// Console.WriteLine("Program started");
//
// // Console.WriteLine($"ThreadId: {Thread.CurrentThread.ManagedThreadId}");
// //
// // Console.WriteLine("Before sleep");
// // Thread.Sleep(5000);
// // Console.WriteLine("After sleep");
//
// // Thread thread = new Thread(() =>
// // {
// // Console.WriteLine("Thread started");
// // // Thread.Sleep(5000);
// //
// // for (int i = 0; i < 5; i++)
// // {
// // Console.WriteLine($"Thread ({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();
// // }
// //
// // for (int i = 0; i < 5; i++)
// // {
// // Console.WriteLine($"Main thread: {i}");
// // Thread.Sleep(250);
// // }
// //
// // Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state before join: {thread.ThreadState}");
// // thread.Join();
// // Console.WriteLine($"Thread ({thread.ManagedThreadId}) current state after join: {thread.ThreadState}");
// //
// // // thread.Start(); // CAUTION: an already started thread can't be started one more time
// //
// // while (threads[0].ThreadState != ThreadState.Stopped)
// // {
// // Console.WriteLine($"Thread ({threads[0].ManagedThreadId}): {threads[0].ThreadState}");
// // threads[0].Join(50);
// //
// // // update your ui
// // }
// //
// // for (int i = 0; i < 10; i++)
// // {
// // Console.WriteLine($"Thread ({threads[i].ManagedThreadId}) current state before join: {threads[i].ThreadState}");
// // threads[i].Join(1000);
// // 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();
// // }
//
// Console.WriteLine("----------- THREADPOOL -----------");
//
// Console.WriteLine($"SetMaxThreads to 1: {ThreadPool.SetMaxThreads(1, 1)}");
// Console.WriteLine($"SetMaxThreads to 20: {ThreadPool.SetMaxThreads(20, 20)}");
//
//
// for (int i = 0; i < 10; i++)
// {
// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep());
// }
//
// Thread.Sleep(4900);
//
// for (int i = 0; i < 10; i++)
// {
// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep());
// }
//
// Console.WriteLine("Program ended");
// }
//
// private static void PrintAndSleep()
// {
// Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} started");
//
// for (int i = 0; i < 10; i++)
// {
// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}");
// Thread.Sleep(500);
// }
//
// Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} stopped");}
// }
// }