送信側
using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Threading; using System.Runtime.InteropServices; public class ShareMemWrite { public const string MEMNAME = "TEST"; public static void Main() { //1.共有メモリの作成 MemoryMappedFile mem = MemoryMappedFile.CreateOrOpen(MEMNAME,512); MemoryMappedViewAccessor acc = mem.CreateViewAccessor(); DateTime d; while(true) { //2.共有メモリの書き込み d = DateTime.Now; acc.Write(1,ref d); Thread.Sleep(1000); } } }
受信側
using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Threading; using System.Runtime.InteropServices; public class ShareMemRead { public const string MEMNAME = "TEST"; public static void Main() { //1.共有メモリの作成 MemoryMappedFile mem = MemoryMappedFile.CreateOrOpen(MEMNAME,512); MemoryMappedViewAccessor acc = mem.CreateViewAccessor(); DateTime d; while(true) { //共有メモリの読み込み acc.Read(1,out d); System.Console.WriteLine(d); Thread.Sleep(1000); } } }