Created
December 27, 2020 14:08
-
-
Save ierhalim/b9f2d64ad46e1afe329bb2e04806b36c to your computer and use it in GitHub Desktop.
Y303 Paralel programlama
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; | |
| using System.IO; | |
| using System.Threading; | |
| namespace LogReader | |
| { | |
| class Program | |
| { | |
| const string exampleLogMutexName = "examplelogMutex"; | |
| static void Main(string[] args) | |
| { | |
| Mutex exampleLogMutex; | |
| try | |
| { | |
| exampleLogMutex = Mutex.OpenExisting(exampleLogMutexName); | |
| } | |
| catch (WaitHandleCannotBeOpenedException) | |
| { | |
| exampleLogMutex = new Mutex(false, exampleLogMutexName); | |
| } | |
| string filePath = @$"{Path.GetTempPath()}\examplelog.txt"; | |
| if (!File.Exists(filePath)) | |
| { | |
| Console.WriteLine("Log file could not be found"); | |
| } | |
| while (true) | |
| { | |
| bool lockTaken = exampleLogMutex.WaitOne(); | |
| string logFileContent = string.Empty; | |
| try | |
| { | |
| logFileContent = File.ReadAllText(filePath); | |
| } | |
| finally | |
| { | |
| if (lockTaken) | |
| { | |
| exampleLogMutex.ReleaseMutex(); | |
| } | |
| } | |
| Console.WriteLine(logFileContent); | |
| Thread.Sleep(1000); | |
| Console.Clear(); | |
| } | |
| } | |
| } | |
| } |
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; | |
| using System.IO; | |
| using System.Threading; | |
| namespace LogWriter | |
| { | |
| class Program | |
| { | |
| const string exampleLogMutexName = "examplelogMutex"; | |
| static void Main(string[] args) | |
| { | |
| Mutex exampleLogMutex; | |
| try | |
| { | |
| // Başka bir uygulama bir mutex oluşturduysa ona erişmeye çalışıyoruz. | |
| exampleLogMutex = Mutex.OpenExisting(exampleLogMutexName); | |
| } | |
| catch (WaitHandleCannotBeOpenedException) | |
| { | |
| // Eğer bir mutex yoksa burada yeni bir mutex oluşturuyoruz. | |
| exampleLogMutex = new Mutex(false, exampleLogMutexName); | |
| } | |
| string filePath = @$"{Path.GetTempPath()}\examplelog.txt"; | |
| if (!File.Exists(filePath)) | |
| { | |
| File.Create(filePath); | |
| } | |
| for (int i = 1000; i > 0; i--) | |
| { | |
| string message = $"Count down: {i}"; | |
| // Eğer başka bir uygulama mutexi kilitlediyse (WaitOne methodunu çalıştırdıysa) bu uygulama diğer Mutex release olana kadar bekleyecek. | |
| // Eğer mutex i bu uygulama kilitlediyse diğer uygulamalar kilidi almak için bekleyecek. | |
| bool lockTaken = exampleLogMutex.WaitOne(); | |
| try | |
| { | |
| File.AppendAllText(filePath, $"{message}\n"); | |
| } | |
| finally | |
| { | |
| if (lockTaken) | |
| { | |
| // İşimiz bittikten sonra eğer lock bizdeyse mutex'i serbest bırakıyoruz. | |
| exampleLogMutex.ReleaseMutex(); | |
| } | |
| } | |
| Console.WriteLine("Log added '{0}'", message); | |
| Thread.Sleep(10); | |
| } | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment