This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Server.cs (перший проект): | |
| using System.Diagnostics; | |
| class GameServer | |
| { | |
| private static int serverScore = 0; | |
| private static int botScore = 0; | |
| static void Main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Threads | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| SimpleThreadDemo(); | |
| // CustomThreadDemo(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyThreads | |
| { | |
| static void Main() | |
| { | |
| Console.WriteLine("Main Thread Id: " + Thread.CurrentThread.ManagedThreadId); | |
| Thread.Sleep(1000); | |
| Console.WriteLine("\n\n"); | |
| new Thread(DoDogs) { IsBackground = false }.Start(); // запуск вторинного пріорітетного потоку | |
| DoDogs(); // запуск методу в первинному потоці |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExampleOfThreadWithState | |
| { | |
| public static void AnotherMethod() | |
| { | |
| for (int i = 0; i < 250; i++) | |
| { | |
| Console.Write("."); | |
| Thread.Sleep(15); | |
| } | |
| Console.WriteLine("AnotherMethod ENDS."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Program | |
| { | |
| static ManualResetEvent pauseEvent = new ManualResetEvent(true); // true - потік одразу активний | |
| static bool isRunning = true; // прапор роботи другого потоку | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Program | |
| { | |
| static object critical = new object(); | |
| static bool run = true; | |
| static void Run(object? nameObj) | |
| { | |
| string name = nameObj + "" ?? "Потік"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; | |
| class ThreadPriorityExample | |
| { | |
| static object locker = new(); | |
| static Random random = new(); | |
| const int STD_OUTPUT_HANDLE = -11; | |
| static IntPtr consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Program | |
| { | |
| static volatile bool duckRoasted = false; // volatile гарантує, що зміни змінної, зроблені одним потоком, одразу стануть видимими іншим потокам | |
| // без volatile компілятор і процесор можуть оптимізувати код так, що один потік (який лайкає пости) читатиме застаріле значення duckRoasted, закешоване в регістрах процесора або кеші ядра CPU | |
| // це може призвести до ситуації, коли duckRoasted уже встановлено в true у roastDuck(), але likeSocialMediaPosts() продовжує працювати безкінечно, не помічаючи зміни | |
| // без volatile компілятор або процесор можуть переставити інструкції місцями, наприклад, записати duckRoasted = true; до виведення "Качка готова!", що може призвести до некоректного порядку подій | |
| // volatile – це простий спосіб уникнути проблем із кешуванням змінної між потоками |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Program | |
| { | |
| static volatile bool duckRoasted = false; | |
| static Timer? checkTimer; | |
| // метод запікання качки з підтримкою колбека | |
| static void RoastDuck(object? param) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| class Program | |
| { | |
| static readonly object fileLock = new object(); | |
| static void RoastDuck(object? state) | |
| { | |
| var dishName = (string?)state; |