Reader/Writer

3 projects: Main project, Reader, Writer.

MainProject, Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MainProject
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(“test.txt”, FileMode.Create);
fs.Close();

Console.WriteLine(“Current context :” + AppDomain.CurrentDomain.Id);

AppDomain dw = AppDomain.CreateDomain(“Writers domain”);
AppDomain dr = AppDomain.CreateDomain(“Readers domain”);

for (int i = 0; i < 20; i++)
{
int opt = new Random().Next(2);
if (opt == 0)
{
dw.ExecuteAssembly(@”..\..\..\Reader\bin\Debug\Reader.exe”);
}
else {
dr.ExecuteAssembly(@”..\..\..\Writer\bin\Debug\Writer.exe”);
}
}

Console.WriteLine(“>>> Press any key to exit… <<<”);
Console.ReadKey();
}
}
}

Reader, Reader.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace Reader
{
class Reader
{
static Mutex localMutex = null;
const string mutexName = “LocalMutex”;
static string path = @”..\..\..\MainProject\bin\Debug\test.txt”;

static void Main(string[] args)
{
//Create the mutex****************************************
try
{
localMutex = Mutex.OpenExisting(mutexName);
}
catch (WaitHandleCannotBeOpenedException)
{
Console.WriteLine(“Mutex is Null!”);
}
if (localMutex == null)
{
localMutex = new Mutex(false, mutexName);
}

new Thread(ReadFromFile).Start();
}

static void ReadFromFile()
{
if (localMutex.WaitOne(100000, false))
{
StreamReader sr = null;
try
{
sr = new StreamReader(path);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Console.WriteLine(“domain no.:” + AppDomain.CurrentDomain.Id + “, Thread:” + Thread.CurrentThread.ManagedThreadId + ” read: ” + sr.ReadToEnd() );
Thread.Sleep(200); // delay necesar observarii apelurilor in paralel
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
finally
{
sr.Close();
localMutex.ReleaseMutex();
}
}
}
}
}

Writer, Writer.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace Writer
{
class Writer
{
static Mutex localMutex;
const string MutexName = “LocalMutex”;
static string path = @”..\..\..\MainProject\bin\Debug\test.txt”;

static void Main(string[] args)
{
localMutex = null;
try
{
localMutex = Mutex.OpenExisting(MutexName);
}
catch (WaitHandleCannotBeOpenedException)
{
Console.WriteLine(“Mutex is Null!”);
}
if (localMutex == null)
{
localMutex = new Mutex(false, MutexName);
}

new Thread(WriteToFile).Start();
}

static void WriteToFile()
{
if (localMutex.WaitOne(100000, false))
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(path,true);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(“domain no.:” + AppDomain.CurrentDomain.Id + “, thread ” + Thread.CurrentThread.ManagedThreadId + ” :: ” + System.DateTime.Now.ToString());
Console.WriteLine(“domain no.:” + AppDomain.CurrentDomain.Id + “, thread ” + Thread.CurrentThread.ManagedThreadId + ” wrote. ” );
Thread.Sleep(200); // delay necesar observarii apelurilor in paralel
sw.Flush();
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
finally
{
sw.Close();
localMutex.ReleaseMutex();
}
}
}
}
}

Leave a Reply