Last active
March 31, 2026 18:45
-
-
Save sunmeat/9a508b3ffe6ef7455b6dad4070a3ae91 to your computer and use it in GitHub Desktop.
використання м'ютекса на рівні 2 процесів
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 WriterA | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| string mutexName = "{D1B62E3A-6C72-4F47-91B5-9C5F5DBA7E89}"; // в VS: средства > создать GUID | |
| using (var mutex = new Mutex(false, mutexName)) | |
| { | |
| while (true) | |
| { | |
| Console.WriteLine("[WriterA] Очікування введення даних..."); | |
| mutex.WaitOne(); // чекаємо, поки інший процес вивільнить м'ютекс | |
| Console.Write("[WriterA] Введіть текст: "); | |
| var input = Console.ReadLine(); | |
| Console.WriteLine($"[WriterA] Ви ввели: {input}"); | |
| Console.WriteLine("[WriterA] Передаю право іншому процесу...\n"); | |
| mutex.ReleaseMutex(); | |
| } | |
| } | |
| } | |
| } | |
| ======================================================================================================= | |
| другий проєкт: | |
| using System.Text; | |
| class WriterB | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| string mutexName = "{D1B62E3A-6C72-4F47-91B5-9C5F5DBA7E89}"; // той самий м'ютекс!!! | |
| using (Mutex mutex = new Mutex(false, mutexName)) | |
| { | |
| while (true) | |
| { | |
| Console.WriteLine("[WriterB] Очікування вводення даних..."); | |
| mutex.WaitOne(); | |
| Console.Write("[WriterB] Введіть текст: "); | |
| var input = Console.ReadLine(); | |
| Console.WriteLine($"[WriterB] Ви ввели: {input}"); | |
| Console.WriteLine("[WriterB] Передаю право іншому процесу...\n"); | |
| mutex.ReleaseMutex(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment