欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#自定義實(shí)現(xiàn)多程序共享內(nèi)存空間

 更新時(shí)間:2024年10月28日 09:18:48   作者:lingxiao16888  
這篇文章主要為大家詳細(xì)介紹了C#如何自定義實(shí)現(xiàn)多程序共享內(nèi)存空間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

創(chuàng)建一個(gè)多個(gè)程序共享的內(nèi)存空間

/// <summary>
    /// 開辟本地自定義的共享內(nèi)存區(qū)域類
    /// </summary>
    public class MCimSharedMemory<T> : IDisposable where T : new()
    {
        /// <summary>
        /// 自定義的內(nèi)存區(qū)域的名
        /// </summary>
        public readonly string MapName = @"Local\CimSharedMemory";
        private MemoryMappedFileSecurity FileSecurity = null;
        private MemoryMappedFile MemoryMapped = null;
        /// <summary>
        /// 可隨機(jī)訪問的內(nèi)存塊
        /// </summary>
        private MemoryMappedViewAccessor MemoryAccessor = null;
        private int Capacity = 0;
 
        public T Value { get; set; }
 
        /// <summary>
        ///  開辟本地自定義的共享內(nèi)存區(qū)域類構(gòu)造方法
        /// </summary>
        public MCimSharedMemory()
        {
            this.FileSecurity = new MemoryMappedFileSecurity();
            this.FileSecurity.AddAccessRule(new AccessRule<MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl, AccessControlType.Allow));
 
            // Memory Map 
            this.Value = new T();
 
            // 獲取結(jié)構(gòu)體大小
            this.Capacity = Marshal.SizeOf(typeof(T));
        }
 
        /// <summary>
        /// 析構(gòu)函數(shù)
        /// </summary>
        ~MCimSharedMemory()
        {
            Dispose();
        }
 
        /// <summary>
        /// 創(chuàng)建并打開自定義的本地共享內(nèi)存區(qū)域
        /// </summary>
        public void CreateOrOpenSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateOrOpen(this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            MemoryMappedFileOptions.None,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
        /// <summary>
        /// 從文件創(chuàng)建自定義的共享內(nèi)存區(qū)域
        /// </summary>
        public void CreateFromFileSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateFromFile(new FileStream(@"", FileMode.Create),
                                            this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable,
                                            true);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
 
       
        public void CreateSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.CreateNew(this.MapName,
                                            this.Capacity,
                                            MemoryMappedFileAccess.ReadWriteExecute,
                                            MemoryMappedFileOptions.None,
                                            this.FileSecurity,
                                            HandleInheritability.Inheritable);
 
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
 
        /// <summary>
        /// 打開自定義的共享內(nèi)存區(qū)域
        /// </summary>
        public void OpenSharedMemory()
        {
            this.MemoryMapped = MemoryMappedFile.OpenExisting(this.MapName);
            this.MemoryAccessor = this.MemoryMapped.CreateViewAccessor();
        }
        /// <summary>
        /// 讀取自定義的共享內(nèi)存區(qū)域
        /// </summary>
        /// <returns></returns>
        public T ReadMemory()
        {
            byte[] bytes = new byte[this.Capacity];
 
            // Initialize unmanaged memory.
            IntPtr p = Marshal.AllocHGlobal(this.Capacity);
 
            // Read from memory mapped file.
            this.MemoryAccessor.ReadArray<byte>(0, bytes, 0, bytes.Length);
 
            // Copy from byte array to unmanaged memory.
            Marshal.Copy(bytes, 0, p, this.Capacity);
 
            // Copy unmanaged memory to struct.
            T ReadData = (T)Marshal.PtrToStructure(p, typeof(T));
 
            // Free unmanaged memory.
            Marshal.FreeHGlobal(p);
            p = IntPtr.Zero;
 
            // Value assign
            this.Value = ReadData;
 
            return ReadData;
        }
        /// <summary>
        /// 向隨機(jī)內(nèi)存寫入值
        /// </summary>
        public void WriteMemory()
        {
            byte[] bytes = new byte[this.Capacity];
 
            // Initialize unmanaged memory.
            IntPtr p = Marshal.AllocHGlobal(this.Capacity);
 
            // Copy struct to unmanaged memory.
            Marshal.StructureToPtr(this.Value, p, false);
 
            // Copy from unmanaged memory to byte array.
            Marshal.Copy(p, bytes, 0, this.Capacity);
 
            this.MemoryAccessor.WriteArray<byte>(0, bytes, 0, bytes.Length);
 
            // Free unmanaged memory.
            Marshal.FreeHGlobal(p);
            p = IntPtr.Zero;
        }
 
        public void Dispose()
        {
            if (this.MemoryAccessor != null)
            {
                this.MemoryAccessor.Dispose();
                this.MemoryAccessor = null;
            }
 
            if (MemoryMapped != null)
            {
                this.MemoryMapped.Dispose();
                this.MemoryMapped = null;
            }
        }
    }
  
 
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
    public class SCimMemory
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xFFFF)] public short[] Bit;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3FFFF)] public short[] Word;
 
        public SCimMemory()
        {
            this.Bit = new short[0xFFFF];
            this.Word = new short[0x3FFFF];
        }
    }

應(yīng)用。

A程序:填充自定義內(nèi)存空間

namespace 共享內(nèi)存區(qū)域1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("共享區(qū)域");
            MCimSharedMemory<SCimMemory> memory = new MCimSharedMemory<SCimMemory>();
            memory.CreateOrOpenSharedMemory();
            Console.WriteLine("寫入值");
            memory.Value = new SCimMemory();
            memory.Value.Bit[0] = 100;
            memory.Value.Word[0] = 120;
            memory.WriteMemory();
            
            Console.WriteLine("輸入完成");
            Console.ReadKey();
        }
    }
}

B程序:讀取自定義共享內(nèi)存數(shù)據(jù)。

namespace 共享內(nèi)存區(qū)域2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("共享區(qū)域");
            MCimSharedMemory<SCimMemory> memory = new MCimSharedMemory<SCimMemory>();
            memory.CreateOrOpenSharedMemory();
            Console.WriteLine("讀取值");
            memory.ReadMemory();
            Console.WriteLine($"B0={memory.Value.Bit[0]},W0={memory.Value.Word[0]}");
 
            Console.WriteLine("輸入完成");
            Console.ReadKey();
        }
    }
}

到此這篇關(guān)于C#自定義實(shí)現(xiàn)多程序共享內(nèi)存空間的文章就介紹到這了,更多相關(guān)C#多程序共享內(nèi)存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c#自帶緩存使用方法 c#移除清理緩存

    c#自帶緩存使用方法 c#移除清理緩存

    這篇文章主要介紹了c#自帶緩存使用方法,包括獲取數(shù)據(jù)緩存、設(shè)置數(shù)據(jù)緩存、移除指定數(shù)據(jù)緩存等方法,需要的朋友可以參考下
    2014-02-02
  • C#獲取Description特性的擴(kuò)展類詳解

    C#獲取Description特性的擴(kuò)展類詳解

    這篇文章主要和大家詳細(xì)介紹一下C#獲取Description特性的擴(kuò)展類,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下
    2022-06-06
  • C#異常處理的技巧和方法

    C#異常處理的技巧和方法

    在本篇文章里小編給大家整理了關(guān)于C#異常處理的技巧和方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-03-03
  • C# 實(shí)現(xiàn)把double 存成兩位精度小數(shù)

    C# 實(shí)現(xiàn)把double 存成兩位精度小數(shù)

    這篇文章主要介紹了C# 實(shí)現(xiàn)把double 存成兩位精度小數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C# List介紹及具體用法

    C# List介紹及具體用法

    這篇文章主要介紹了C# List介紹及具體用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • c#棧變化規(guī)則圖解示例(棧的生長(zhǎng)與消亡)

    c#棧變化規(guī)則圖解示例(棧的生長(zhǎng)與消亡)

    多數(shù)情況下我們不需要關(guān)心棧的變化,下文會(huì)給出一個(gè)具體的示例。另外,理解棧的變化對(duì)于理解作用域也有一定的好處,因?yàn)镃#的局部變量作用域是基于棧的。
    2013-11-11
  • C#自定義基于控制臺(tái)的Timer實(shí)例

    C#自定義基于控制臺(tái)的Timer實(shí)例

    這篇文章主要介紹了C#自定義基于控制臺(tái)的Timer實(shí)現(xiàn)方法,可以簡(jiǎn)單模擬timer控件的相關(guān)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C# Winform TextBox控件多行輸入方式

    C# Winform TextBox控件多行輸入方式

    這篇文章主要介紹了C# Winform TextBox控件多行輸入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • WinForm開發(fā)中屏蔽WebBrowser腳本錯(cuò)誤提示的方法

    WinForm開發(fā)中屏蔽WebBrowser腳本錯(cuò)誤提示的方法

    這篇文章主要介紹了WinForm開發(fā)中屏蔽WebBrowser腳本錯(cuò)誤提示的方法,在C#項(xiàng)目開發(fā)中比較實(shí)用,需要的朋友可以參考下
    2014-08-08
  • C#使用ILGenerator動(dòng)態(tài)生成函數(shù)的簡(jiǎn)單代碼

    C#使用ILGenerator動(dòng)態(tài)生成函數(shù)的簡(jiǎn)單代碼

    這篇文章主要介紹了C#使用ILGenerator動(dòng)態(tài)生成函數(shù)的簡(jiǎn)單代碼,需要的朋友可以參考下
    2017-08-08

最新評(píng)論