送信側
using System; using System.IO; using System.Threading; public class FileWrite { private static Mutex mut = new Mutex(false,"mut"); private const string FILE_NAME = "hoge.txt"; public static void Main(string[] args) { while(true) { //1.処理 Thread.Sleep(1000); //2.ロック mut.WaitOne(); //3.書き込み DateTime d = DateTime.Now; StreamWriter sw = new StreamWriter(FILE_NAME); sw.WriteLine(d.ToString()); sw.Close(); //4.ロックの開放 mut.ReleaseMutex(); } } }
受信側
using System; using System.IO; using System.Threading; public class FileRead { private static Mutex mut = new Mutex(false,"mut"); private const string FILE_NAME = "hoge.txt"; public static void Main(string[] args) { while(true) { //1.ロック mut.WaitOne(); //2.読み込み StreamReader sr = new StreamReader(FILE_NAME); Console.WriteLine(sr.ReadLine()); sr.Close(); //3.ロックの開放 mut.ReleaseMutex(); //4.処理 Thread.Sleep(1000); } } }
0 件のコメント:
コメントを投稿