久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C#內(nèi)存映射文件學(xué)習(xí)

 長江黃鶴 2014-07-31

內(nèi)存映射文件是由一個文件到進程地址空間的映射,。

        C#提供了允許應(yīng)用程序把文件映射到一個進程的函(MemoryMappedFile.CreateOrOpen),。內(nèi)存映射文件與虛擬內(nèi)存有些類似,通過內(nèi)存映射文件可以保留一個地址空間的區(qū)域,,同時將物理存儲器提交給此區(qū)域,,只是內(nèi)存文件映射的物理存儲器來自一個已經(jīng)存在于磁盤上的文件,而非系統(tǒng)的頁文件,,而且在對該文件進行操作之前必須首先對文件進行映射,,就如同將整個文件從磁盤加載到內(nèi)存。由此可以看出,,使用內(nèi)存映射文件處理存儲于磁盤上的文件時,,將不必再對文件執(zhí)行I/O操作,這意味著在對文件進行處理時將不必再為文件申請并分配緩存,,所有的文件緩存操作均由系統(tǒng)直接管理,,由于取消了將文件數(shù)據(jù)加載到內(nèi)存、數(shù)據(jù)從內(nèi)存到文件的回寫以及釋放內(nèi)存塊等步驟,,使得內(nèi)存映射文件在處理大數(shù)據(jù)量的文件時能起到相當(dāng)重要的作用,。另外,實際工程中的系統(tǒng)往往需要在多個進程之間共享數(shù)據(jù),,如果數(shù)據(jù)量小,,處理方法是靈活多變的,,如果共享數(shù)據(jù)容量巨大,那么就需要借助于內(nèi)存映射文件來進行,。實際上,,內(nèi)存映射文件正是解決本地多個進程間數(shù)據(jù)共享的最有效方法。

        共享內(nèi)存是內(nèi)存映射文件的一種特殊情況,,內(nèi)存映射的是一塊內(nèi)存,,而非磁盤上的文件。共享內(nèi)存的主語是進程(Process),,操作系統(tǒng)默認會給每一個進程分配一個內(nèi)存空間,,每一個進程只允許訪問操作系統(tǒng)分配給它的哪一段內(nèi)存,而不能訪問其他進程的,。而有時候需要在不同進程之間訪問同一段內(nèi)存,,怎么辦呢?操作系統(tǒng)給出了創(chuàng)建訪問共享內(nèi)存的API,,需要共享內(nèi)存的進程可以通過這一組定義好的API來訪問多個進程之間共有的內(nèi)存,,各個進程訪問這一段內(nèi)存就像訪問一個硬盤上的文件一樣。而.Net 4.0中引入了System.IO.MemoryMappedFiles命名空間,,這個命名空間的類對windows 共享內(nèi)存相關(guān)API做了封裝,,使.Net程序員可以更方便的使用內(nèi)存映射文件。

內(nèi)存映射文件實現(xiàn)進程間通訊

        內(nèi)存映射文件是實現(xiàn)進程通訊的又一種方法,,我們可以通過共享剪貼板,、共享物理文件來實現(xiàn)進程間的數(shù)據(jù)共享,這里我們還可以通過內(nèi)存映射文件來實現(xiàn)共享,,這樣,,文件內(nèi)的數(shù)據(jù)就可以用內(nèi)存讀/寫指令來訪問,而不是用ReadFile和WriteFile這樣的I/O系統(tǒng)函數(shù),,從而提高了文件存取速度。這種方式更加快捷高效,,最適用于需要讀取文件并且對文件內(nèi)包含的信息做語法分析的應(yīng)用程序,,如:對輸入文件進行語法分析的彩色語法編輯器,編譯器等,。這種數(shù)據(jù)共享是讓兩個或多個進程映射同一文件映射對象的視圖,,即它們在共享同一物理存儲頁。這樣,,當(dāng)一個進程向內(nèi)存映射文件的一個視圖寫入數(shù)據(jù)時,,其他的進程立即在自己的視圖中看到變化。

注意:

        對文件映射對象要使用同一名字,。

        是讓兩個或多個進程映射同一文件映射對象的視圖,,即它們在共享同一物理存儲頁,。這樣,當(dāng)一個進程向內(nèi)存映射文件的一個視圖寫入數(shù)據(jù)時,,其他的進程立即在自己的視圖中看到變化,。但要注意,對文件映射對象要使用同一名字,。

 內(nèi)存映射文件使用實例:

1.      在同一進程內(nèi)同時讀寫同一內(nèi)存映射文件

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.IO.MemoryMappedFiles;  
  11.  
  12.  
  13. namespace UseMMFInProcess  
  14. {  
  15.     public partial class frmMain : Form  
  16.     {  
  17.         public frmMain()  
  18.         {  
  19.             InitializeComponent();  
  20.             CreateMemoryMapFile();  
  21.         }  
  22.         private const int FILE_SIZE = 512;  
  23.         /// <summary>  
  24.         /// 引用內(nèi)存映射文件  
  25.         /// </summary>  
  26.         private MemoryMappedFile memoryFile = null;  
  27.         /// <summary>  
  28.         /// 用于訪問內(nèi)存映射文件的存取對象  
  29.         /// </summary>  
  30.         private MemoryMappedViewAccessor accessor1, accessor2,accessor;  
  31.         /// <summary>  
  32.         /// 創(chuàng)建內(nèi)存映射文件  
  33.         /// </summary>  
  34.         private void CreateMemoryMapFile()  
  35.         {  
  36.             try 
  37.             {       
  38.                 memoryFile = MemoryMappedFile.CreateFromFile("MyFile.dat", FileMode.OpenOrCreate, "MyFile", FILE_SIZE);                 
  39.                 //訪問文件前半段  
  40.                 accessor1 = memoryFile.CreateViewAccessor(0, FILE_SIZE / 2);              
  41.                 //訪問文件后半段  
  42.                 accessor2 = memoryFile.CreateViewAccessor(FILE_SIZE / 2, FILE_SIZE / 2);                
  43.                 //訪問全部文件  
  44.                 accessor = memoryFile.CreateViewAccessor();  
  45.                 //InitFileContent();  
  46.                 lblInfo.Text = "內(nèi)存文件創(chuàng)建成功";  
  47.                 ShowFileContents();  
  48.             }  
  49.             catch (Exception ex)  
  50.             {  
  51.                 lblInfo.Text = ex.Message;  
  52.             }  
  53.         }  
  54.         /// <summary>  
  55.         /// 關(guān)閉并釋放資源  
  56.         /// </summary>  
  57.         private void DisposeMemoryMapFile()  
  58.         {  
  59.             if (accessor1 != null)  
  60.                 accessor1.Dispose();  
  61.             if (accessor2 != null)  
  62.                 accessor2.Dispose();  
  63.             if (memoryFile != null)  
  64.                 memoryFile.Dispose();  
  65.         }  
  66.  
  67.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)  
  68.         {  
  69.             DisposeMemoryMapFile();  
  70.         }  
  71.  
  72.         private void btnWrite1_Click(object sender, EventArgs e)  
  73.         {  
  74.             if (textBox1.Text.Length == 0)  
  75.             {  
  76.                 lblInfo.Text = "請輸入一個字符";  
  77.                 return;  
  78.             }  
  79.             char[] chs = textBox1.Text.ToCharArray();  
  80.             char ch = chs[0];  
  81.              
  82.             for (int i = 0; i < FILE_SIZE / 2; i += 2)  
  83.                 accessor1.Write(i, ch);  
  84.               
  85.             lblInfo.Text = "字符“" + ch + "”已寫到文件前半部份";  
  86.             ShowFileContents();  
  87.         }  
  88.  
  89.         private void btnShow_Click(object sender, EventArgs e)  
  90.         {  
  91.             ShowFileContents();  
  92.         }  
  93.  
  94.         /// <summary>  
  95.         /// 初始化文件內(nèi)容為可視的字符“0”  
  96.         /// </summary>  
  97.         private void InitFileContent()  
  98.         {  
  99.             for (int i = 0; i < FILE_SIZE; i += 2)   
  100.                 accessor.Write(i,'0');  
  101.         }  
  102.         /// <summary>  
  103.         /// 顯示文件內(nèi)容  
  104.         /// </summary>  
  105.         private void ShowFileContents()  
  106.         {  
  107.             StringBuilder sb = new StringBuilder(FILE_SIZE);  
  108.             sb.Append("上半段內(nèi)容:\n");  
  109.  
  110.             int j = 0;  
  111.             for (int i = 0; i < FILE_SIZE / 2; i += 2)  
  112.             {  
  113.                 sb.Append("\t");  
  114.                 char ch = accessor.ReadChar(i);  
  115.                 sb.Append(j);  
  116.                 sb.Append(":");  
  117.                 sb.Append(ch);  
  118.                 j++;  
  119.             }  
  120.             sb.Append("\n下半段內(nèi)容:\n");  
  121.  
  122.             for (int i = FILE_SIZE / 2; i < FILE_SIZE; i += 2)  
  123.             {  
  124.                 sb.Append("\t");  
  125.                 char ch = accessor.ReadChar(i);  
  126.                 sb.Append(j);  
  127.                 sb.Append(":");  
  128.                 sb.Append(ch);  
  129.                 j++;  
  130.             }  
  131.             richTextBox1.Text = sb.ToString();  
  132.         }  
  133.  
  134.         private void btnWrite2_Click(object sender, EventArgs e)  
  135.         {  
  136.             if (textBox2.Text.Length == 0)  
  137.             {  
  138.                 lblInfo.Text = "請輸入一個字符";  
  139.                 return;  
  140.             }  
  141.             char[] chs = textBox2.Text.ToCharArray();  
  142.             char ch = chs[0];  
  143.  
  144.             for (int i = 0; i < FILE_SIZE / 2; i += 2)  
  145.                 accessor2.Write(i, ch);  
  146.             lblInfo.Text = "字符“" + ch + "”已寫到文件后半部份";  
  147.             ShowFileContents();  
  148.         }  
  149.     }  

2.      使用內(nèi)存映射文件在進程間傳送值類型數(shù)據(jù)

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace UseMMFBetweenProcess  
  7. {  
  8.     /// <summary>  
  9.     /// 要共享的數(shù)據(jù)結(jié)構(gòu),,注意,其成員不能是引用類型  
  10.     /// </summary>  
  11.     public struct MyStructure  
  12.     {  
  13.         public int IntValue  
  14.         {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         public float FloatValue  
  19.         {  
  20.             get;  
  21.             set;  
  22.         }   
  23.     }   
  24. }  
  25.  
  26.  
  27. using System;  
  28. using System.Collections.Generic;  
  29. using System.ComponentModel;  
  30. using System.Data;  
  31. using System.Drawing;  
  32. using System.Linq;  
  33. using System.Text;  
  34. using System.Windows.Forms;  
  35. using System.IO.MemoryMappedFiles;  
  36. using System.IO;  
  37.  
  38. namespace UseMMFBetweenProcess  
  39. {  
  40.     public partial class frmMain : Form  
  41.     {  
  42.         public frmMain()  
  43.         {  
  44.             InitializeComponent();  
  45.             InitMemoryMappedFile();  
  46.         }  
  47.  
  48.         /// <summary>  
  49.         /// 內(nèi)存映射文件的容量  
  50.         /// </summary>  
  51.         private const int FileSize = 1024 * 1024;  
  52.         private MemoryMappedFile file = null;  
  53.         private MemoryMappedViewAccessor accessor = null;  
  54.  
  55.         /// <summary>  
  56.         /// 初始化內(nèi)存映射文件  
  57.         /// </summary>  
  58.         private void InitMemoryMappedFile()  
  59.         {  
  60.             file = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess", FileSize);  
  61.             accessor = file.CreateViewAccessor();  
  62.             lblInfo.Text = "內(nèi)存文件創(chuàng)建或連接成功";           
  63.         }  
  64.  
  65.         /// <summary>  
  66.         /// 要共享的數(shù)據(jù)對象  
  67.         /// </summary>  
  68.         private MyStructure data;  
  69.  
  70.         /// <summary>  
  71.         /// 顯示數(shù)據(jù)到窗體上  
  72.         /// </summary>  
  73.         private void ShowData()  
  74.         {  
  75.             textBox1.Text = data.IntValue.ToString();  
  76.             textBox2.Text = data.FloatValue.ToString();  
  77.         }  
  78.  
  79.         /// <summary>  
  80.         /// 根據(jù)用戶輸入更新數(shù)據(jù)  
  81.         /// </summary>  
  82.         private void UpdateData()  
  83.         {  
  84.             data.IntValue = int.Parse(textBox1.Text);  
  85.             data.FloatValue = float.Parse(textBox2.Text);  
  86.         }  
  87.  
  88.         private void btnSave_Click(object sender, EventArgs e)  
  89.         {  
  90.             try 
  91.             {  
  92.                 UpdateData();  
  93.                 accessor.Write<MyStructure>(0, ref data);  
  94.                 lblInfo.Text = "數(shù)據(jù)已經(jīng)保存到內(nèi)存文件中";  
  95.             }  
  96.             catch (Exception ex)  
  97.             {  
  98.                 lblInfo.Text = ex.Message;  
  99.             }  
  100.         }  
  101.  
  102.         private void btnLoad_Click(object sender, EventArgs e)  
  103.         {  
  104.             accessor.Read<MyStructure>(0, out data);  
  105.             ShowData();  
  106.             lblInfo.Text = "成功從內(nèi)存文件中提取了數(shù)據(jù)";  
  107.         }  
  108.  
  109.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)  
  110.         {  
  111.             if (accessor != null)  
  112.                 accessor.Dispose();  
  113.             if (file != null)  
  114.                 file.Dispose();  
  115.         }  
  116.     }  

3.      利用序列化技術(shù)通過內(nèi)存映射文件實現(xiàn)進程通訊

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.IO.MemoryMappedFiles;  
  11. using System.Runtime.Serialization;  
  12. using System.Runtime.Serialization.Formatters.Binary;  
  13.  
  14. namespace UseMMFBetweenProcess2  
  15. {  
  16.     public partial class frmMain : Form  
  17.     {  
  18.         public frmMain()  
  19.         {  
  20.             InitializeComponent();  
  21.             InitMemoryMappedFile();  
  22.         }  
  23.          
  24.         /// <summary>  
  25.         /// 圖片  
  26.         /// </summary>  
  27.         private Image bmp  
  28.         {  
  29.             get 
  30.             {  
  31.                 return pictureBox1.Image;  
  32.             }  
  33.             set 
  34.             {  
  35.                 pictureBox1.Image = value;  
  36.             }  
  37.         }  
  38.          
  39.        /// <summary>  
  40.        /// 圖片說明  
  41.        /// </summary>  
  42.         private string info  
  43.         {  
  44.             get 
  45.             {  
  46.                 return txtImageInfo.Text;  
  47.             }  
  48.             set 
  49.             {  
  50.                 txtImageInfo.Text = value;  
  51.             }  
  52.         }  
  53.  
  54.         private MemoryMappedFile memoryFile = null;  
  55.  
  56.         private MemoryMappedViewStream stream = null;  
  57.  
  58.         /// <summary>  
  59.         /// 最大容量:10M  
  60.         /// </summary>  
  61.         private const int FileSize = 1024 * 1024 * 10;    
  62.  
  63.         /// <summary>  
  64.         /// 創(chuàng)建內(nèi)存映射文件,獲取其讀寫流  
  65.         /// </summary>  
  66.         private void InitMemoryMappedFile()  
  67.         {  
  68.             try 
  69.             {  
  70.   memoryFile = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess2", FileSize);  
  71.             stream = memoryFile.CreateViewStream();  
  72.             }  
  73.             catch (Exception ex )  
  74.             {  
  75.                 MessageBox.Show(ex.Message);  
  76.                 Close();  
  77.             }  
  78.         }  
  79.         /// <summary>  
  80.         /// 釋放相關(guān)資源  
  81.         /// </summary>  
  82.         private void DisposeMemoryMappedFile()  
  83.         {  
  84.             if (stream != null)  
  85.                 stream.Close();  
  86.             if (memoryFile != null)  
  87.                 memoryFile.Dispose();  
  88.         }  
  89.  
  90.         private void btnLoadPic_Click(object sender, EventArgs e)  
  91.         {  
  92.             ChooseImageFile();  
  93.         }  
  94.  
  95.         //選擇圖片  
  96.         private void ChooseImageFile()  
  97.         {  
  98.             if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  99.             {  
  100.                 bmp = new Bitmap(openFileDialog1.FileName);  
  101.             }  
  102.         }  
  103.         //根據(jù)用戶設(shè)定的信息創(chuàng)建對象  
  104.         private MyPic CreateMyPicObj()  
  105.         {  
  106.             MyPic obj = new MyPic();  
  107.             obj.pic = bmp;  
  108.             obj.picInfo = info;  
  109.             return obj;  
  110.         }  
  111.  
  112.         /// <summary>  
  113.         /// 將MyPic對象保存到內(nèi)存映射文件中  
  114.         /// </summary>  
  115.         private void SaveToMMF()  
  116.         {  
  117.             try 
  118.             {  
  119.             MyPic obj = CreateMyPicObj();  
  120.             IFormatter formatter = new BinaryFormatter();  
  121.             stream.Seek(0, SeekOrigin.Begin);  
  122.             formatter.Serialize(stream, obj);  
  123.             MessageBox.Show("對象已保存到內(nèi)存映射文件中");  
  124.             }  
  125.             catch (Exception ex)  
  126.             {  
  127.                 MessageBox.Show(ex.Message);  
  128.             }  
  129.         }  
  130.  
  131.  private void LoadFromMMF()  
  132.         {  
  133.             try 
  134.             {  
  135.            // CreateMyPicObj();  
  136.             IFormatter formatter = new BinaryFormatter();  
  137.             stream.Seek(0, SeekOrigin.Begin);  
  138.             MyPic obj =   formatter.Deserialize(stream) as MyPic;  
  139.             if (obj != null)  
  140.             {  
  141.                 bmp = obj.pic;  
  142.                 info = obj.picInfo;  
  143.             }  
  144.           }  
  145.           catch (Exception ex)  
  146.           {  
  147.               MessageBox.Show(ex.Message);  
  148.           }  
  149.         }  
  150.  
  151.         private void btnExit_Click(object sender, EventArgs e)  
  152.         {  
  153.             Close();  
  154.         }  
  155.  
  156.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)  
  157.         {  
  158.             DisposeMemoryMappedFile();  
  159.         }  
  160.  
  161.         private void btnSaveToMMF_Click(object sender, EventArgs e)  
  162.         {  
  163.             SaveToMMF();  
  164.         }  
  165.  
  166.         private void btnLoadFromMMF_Click(object sender, EventArgs e)  
  167.         {  
  168.             LoadFromMMF();  
  169.         }  
  170.     }  

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多