.Net 利用MemoryMappedFile实现进程间相互通信

      在.NET中有这样一个类MemoryMappedFile,顾名思义:内存文件映射.

这个类可以很方便的将硬盘中的物理文件,映射到内存中,操作文件时,就可以直接从内存中读取写入了。省去了磁盘转入内存,内存转磁盘的操作时间,效率当然也是更高。

这适合用于大文件频繁的读写,效果比较显著。

除了从已有的物理文件加载数据源(MemoryMappedFile.CreateFromFile),也支持在内存中直接创建数据(MemoryMappedFile.CreateOrOpen),

直接在内存中创建数据并保留.并且这段内存是特殊的,是可以共享的,即:共享内存。

在同一台PC,运行的软件(进程),系统都会给其分配一段内存供其调度运行.每个进程独占这段内存,互不干扰。那么共享内存,就好比一个公共区域,

每个进程都可以申请,使用。MemoryMappedFile正是具有这个特性,所以可以实现进程间的相互通信.

在同一PC种,利用它实现进程间通信,是非常方便且高效的!

      这里自己做了个简单DEMO,旨意阐述MemoryMappedFile具有实现进程间通信的功能.

完整代码如下:

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Collections.Specialized;
  5 using System.Linq;
  6 using System.Net;
  7 using System.Net.Sockets;
  8 using System.Text;
  9 using System.Threading;
 10 using System.Threading.Tasks;
 11 using System.Net.NetworkInformation;
 12 using System.IO;
 13 using System.IO.Compression;
 14 using ICSharpCode.SharpZipLib.Zip;
 15 using System.IO.MemoryMappedFiles;
 16 using System.Runtime.InteropServices;
 17 
 18 namespace ConsoleApplication3
 19 {
 20     class Program
 21     {
 22         static void Main(string[] args)
 23         {
 24             Console.WriteLine("注: w:写 r:读");
 25             while (true)
 26             {
 27                 string ac = Console.ReadLine();
 28 
 29                 if (ac == "w")
 30                 {
 31                     Console.WriteLine("请输入存入内容:");
 32                     string content = Console.ReadLine();
 33                     if (!string.IsNullOrEmpty(content))
 34                     {
 35                         WriteMMF("mmf001", content);
 36                     }
 37                     else {
 38                         Console.WriteLine("存入内容不能为空!");
 39                     }
 40                 }
 41                 else if (ac == "r")
 42                 {
 43                     ReadMMF("mmf001");
 44                 }
 45                 else {
 46 
 47                     Console.WriteLine("注: w:写 r:读");
 48                 }
 49             }
 50            
 51         }
 52         /// <summary>
 53         /// 写入映射文件
 54         /// </summary>
 55         /// <param name="mapname"></param>
 56         /// <param name="content"></param>
 57         static void WriteMMF(string mapname,string content) {
 58 
 59             MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(mapname,1000, MemoryMappedFileAccess.ReadWrite);
 60             if (!string.IsNullOrEmpty(content))
 61             {
 62                 using (var mmfStream = mmf.CreateViewStream())
 63                 {
 64                     using (var sw = new StreamWriter(mmfStream))
 65                     {
 66                         sw.Write(content.Trim());
 67                     }
 68                 }
 69 
 70                 Console.WriteLine("保存成功!");
 71             }
 72         }
 73 
 74         /// <summary>
 75         /// 读取映射文件
 76         /// </summary>
 77         /// <param name="mapname"></param>
 78         static void ReadMMF(string mapname)
 79         {
 80             MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname);
 81             using (var mmfStream = mmf.CreateViewStream(0,1000, MemoryMappedFileAccess.ReadWrite))
 82             {
 83                 byte[] buffer = new byte[128];
 84                 int nLength = 0;
 85                 StringBuilder sb = new StringBuilder();
 86                 do
 87                 {
 88                     nLength = mmfStream.Read(buffer, 0, 128);
 89                     sb.AppendLine(System.Text.ASCIIEncoding.Default.GetString(buffer));
 90 
 91                 } while (nLength > 0);
 92                
 93                 Console.WriteLine("读取内容:" + sb.ToString().Replace("\0", null).TrimEnd());
 94             }
 95             Console.WriteLine("读取成功!");
 96         }
 97 
 98     }
 99 
100    
101 
102 }
View Code

测试效果图:

 

posted on 2019-02-13 17:18  潇潇@暮雨  阅读(3216)  评论(0编辑  收藏  举报

导航