[Visual C#] .NET Framework自带的文件内存映射类(保存留以后用)

最近一直为文件内存映射发愁,整个两周一直折腾这个东西。在64位系统和32位系统还要针对内存的高低位进行计算。好麻烦。。还是没搞定

偶然从MSDN上发现.NET 4.0把内存文件映射加到了.NET类库中。。好像方便了很多啊。。比用C#直接调用WINDOWS API方便多了。所以

这个必须果断记录之。。。项目马上要用,为了加强内存数据交换的效率。。这个。。。必须啊。。

 

任务

使用的方法或属性

从磁盘上的文件中获取表示持久内存映射文件的 MemoryMappedFile 对象。

MemoryMappedFile.CreateFromFile 方法。

获取表示非持久内存映射文件(与磁盘上的文件不关联)的 MemoryMappedFile 对象。

MemoryMappedFile.CreateNew 方法。

- 或 -

MemoryMappedFile.CreateOrOpen 方法。

获取现有内存映射文件(持久文件或非持久文件)的 MemoryMappedFile 对象。

MemoryMappedFile.OpenExisting 方法。

获取针对内存映射文件的顺序访问视图的 UnmanagedMemoryStream 对象。

MemoryMappedFile.CreateViewStream 方法。

获取针对内存映射文件的随机访问视图的 UnmanagedMemoryAccessor 对象。

MemoryMappedFile.CreateViewAccessor 方法。

获取要用于非托管代码的 SafeMemoryMappedViewHandle 对象。

MemoryMappedFile.SafeMemoryMappedFileHandle 属性。

- 或 -

MemoryMappedViewAccessor.SafeMemoryMappedViewHandle 属性。

- 或 -

MemoryMappedViewStream.SafeMemoryMappedViewHandle 属性。

将内存分配推迟到创建视图后进行(仅限于非持久文件)。

(若要确定当前系统页大小,请使用 Environment.SystemPageSize 属性。)

 MemoryMappedFileOptions.DelayAllocatePages 值的 CreateNew 方法。

- 或 -

 MemoryMappedFileOptions 枚举作为参数的 CreateOrOpen 方法。

 

 

持久文件内存映射:

CreateFromFile 方法基于磁盘上的现有文件创建一个内存映射文件。

 1 using System;
 2 using System.IO;
 3 using System.IO.MemoryMappedFiles;
 4 using System.Runtime.InteropServices;
 5 
 6 class Program
 7 {
 8     static void Main(string[] args)
 9     {
10         long offset = 0x10000000// 256 megabytes
11         long length = 0x20000000// 512 megabytes
12 
13         // Create the memory-mapped file.
14         using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
15         {
16             // Create a random access view, from the 256th megabyte (the offset)
17             // to the 768th megabyte (the offset plus length).
18             using (var accessor = mmf.CreateViewAccessor(offset, length))
19             {
20                 int colorSize = Marshal.SizeOf(typeof(MyColor));
21                 MyColor color;
22 
23                 // Make changes to the view.
24                 for (long i = 0; i < length; i += colorSize)
25                 {
26                     accessor.Read(i, out color);
27                     color.Brighten(10);
28                     accessor.Write(i, ref color);
29                 }
30             }
31         }
32     }
33 }
34 
35 public struct MyColor
36 {
37     public short Red;
38     public short Green;
39     public short Blue;
40     public short Alpha;
41 
42     // Make the view brigher.
43     public void Brighten(short value)
44     {
45         Red = (short)Math.Min(short.MaxValue, (int)Red + value);
46         Green = (short)Math.Min(short.MaxValue, (int)Green + value);
47         Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
48         Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
49     }
50 }

 非持久文件内存映射:

CreateNew 和 CreateOrOpen 方法创建一个未映射到磁盘上的现有文件的内存映射文件。

 

 1 using System;
 2 using System.IO;
 3 using System.IO.MemoryMappedFiles;
 4 using System.Threading;
 5 
 6 class Program
 7 {
 8     // Process A:
 9     static void Main(string[] args)
10     {
11         using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap"10000))
12         {
13             bool mutexCreated;
14             Mutex mutex = new Mutex(true"testmapmutex"out mutexCreated);
15             using (MemoryMappedViewStream stream = mmf.CreateViewStream())
16             {
17                 BinaryWriter writer = new BinaryWriter(stream);
18                 writer.Write(1);
19             }
20             mutex.ReleaseMutex();
21 
22             Console.WriteLine("Start Process B and press ENTER to continue.");
23             Console.ReadLine();
24 
25             Console.WriteLine("Start Process C and press ENTER to continue.");
26             Console.ReadLine();
27 
28             mutex.WaitOne();
29             using (MemoryMappedViewStream stream = mmf.CreateViewStream())
30             {
31                 BinaryReader reader = new BinaryReader(stream);
32                 Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
33                 Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
34                 Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
35             }
36             mutex.ReleaseMutex();
37         }
38     }
39 }

 

 

posted @ 2012-11-25 01:30  star丶清影  阅读(3285)  评论(0编辑  收藏  举报