金沙国际官网_金沙国际平台登录

因为这个金沙国际官网_金沙国际平台登录网站与很多的大型澳门赌场都有合作,金沙国际官网_金沙国际平台登录尽职尽责,高效执行,保持好奇心,不断学习,追求卓越,点击进入金沙国际官网_金沙国际平台登录马上体验吧,所以现在也正式地开始了营业。

您的位置:金沙国际官网 > 编程 > 内存映射大文件并使用Marshal解析结构体信息

内存映射大文件并使用Marshal解析结构体信息

发布时间:2019-11-02 08:48编辑:编程浏览(88)

     


    内部存款和储蓄器映射数据管理类入眼函数及变量如下:

    对此Action的选用形式运用如下:

     

     1        string _filepath;
     2        /// <summary>
     3         /// 引用内存映射文件
     4         /// </summary>
     5         private MemoryMappedFile _memoryFile = null;
     6         /// <summary>
     7         /// 用于访问内存映射文件的存取对象
     8         /// </summary>
     9         private MemoryMappedViewAccessor _accessor = null;
    10         public ScientificData _ScientificData = new ScientificData();
    11         long _lenByte = 0;
    12         public DatFileInfo(string filepath)
    13         {
    14             _filepath = filepath;
    15             _memoryFile = MemoryMappedFile.CreateFromFile(_filepath);
    16             _accessor = _memoryFile.CreateViewAccessor();
    17             // _stream = _memoryFile.CreateViewStream();
    18             FileInfo finfo = new FileInfo(filepath);
    19             _lenByte = finfo.Length;//文件字节大小
    20         }
    21         public void SaveRawData(string savepath)
    22         {
    23             
    24             int currentByteNum = 0;//当前字节位置
    25             uint ACountint = 0;
    26             uint RCountint = 0;
    27             ScientificData scientificData = new ScientificData();
    28             byte[] data = new byte[1036 * 1036];
    29             while (currentByteNum<= (_lenByte- 1036 * 1036))
    30             {
    31                 _accessor.Read<uint>(currentByteNum, out RCountint);
    32                 _accessor.Read<uint>(currentByteNum+4, out ACountint);
    33                 if (RCountint < 1400 && ACountint < 1401 && _accessor.ReadByte(currentByteNum+8)==0x0a && _accessor.ReadByte(currentByteNum + 9) == 0x0b)//初步判断条件,节省解析结构体时间
    34                 {
    35                     _accessor.ReadArray(currentByteNum, data, 0, data.Length);//读取结构体数据到字节数组
    36                     scientificData = ByteToStructure<ScientificData>(data);//字节数组解析到结构体
    37                     if((scientificData.aux_3a1 == 0x3A) && (scientificData.aux_3a3 == 0x3A))//进一步判断
    38                     {
    39                         ushort[,] sdata = scientificData.GetImageData();//得到所需的数据
    40                         saveRawData(savepath + ((int)((ACountint - 1)/15+1)).ToString()+ "_" + (ACountint-1).ToString() + "_"+ACountint + "_"+scientificData.aux_num + ".raw" , sdata);
    41                         currentByteNum += 1036 * 1036;
    42                     }
    43                     else
    44                         currentByteNum++;
    45                 }
    46                 else
    47                     currentByteNum++;
    48 
    49 
    50             }
    51         }
    52         /// <summary>
    53         /// 由byte数组转换为结构体
    54         /// </summary>
    55         public static T ByteToStructure<T>(byte[] dataBuffer)
    56         {
    57             object structure = null;
    58             int size = Marshal.SizeOf(typeof(T));
    59             IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
    60             try
    61             {
    62                 Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
    63                 structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
    64             }
    65             finally
    66             {
    67                 Marshal.FreeHGlobal(allocIntPtr);
    68             }
    69             return (T)structure;
    70         }
    71         private void saveRawData(string savepath,ushort[,] data)
    72         {
    73             int len = data.Length*2;
    74             byte[] bdata = new byte[len];
    75             Buffer.BlockCopy(data,0,bdata,0,len);
    76             File.WriteAllBytes(savepath, bdata);
    77         }
    78         /// <summary>
    79         /// 由结构体转换为byte数组
    80         /// </summary>
    81         public static byte[] StructureToByte<T>(T structure)
    82         {
    83             int size = Marshal.SizeOf(typeof(T));
    84             byte[] buffer = new byte[size];
    85             IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
    86             try
    87             {
    88                 Marshal.StructureToPtr(structure, bufferIntPtr, true);
    89                 Marshal.Copy(bufferIntPtr, buffer, 0, size);
    90             }
    91             finally
    92             {
    93                 Marshal.FreeHGlobal(bufferIntPtr);
    94             }
    95             return buffer;
    96         }
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string first = "First";
                var action = new Action(() => { Console.WriteLine(first); });
                action();
    
                var action2 = new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); });
                action2(first);
    
                var action3 = new Action<string, string>((s1, s2) => {
                    Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
                });
                action3(first, "second");
            }
        }
    }
    

    委托概述

    将艺术调用者和对象措施动态关联起来,委托是三个类,所以它和类是同级的,可以透过信托来掉用艺术,不要误感觉委托和办法同级的,方法只是类的成员。委托定义了章程的档期的顺序(定义委托和与之对应的章程必需有所同等的参数个数,况且类型相符,重返值类型相近),使得可以将艺术充作另三个办法的参数来进展传递,这种将艺术动态地赋给参数的做法,可以幸免在程序中山大学量施用If-Else(Switch)语句,同一时候使得程序有所更加好的可增添性。

     

    是的数据结构体定义如下:

    行使dotPeek通过反编写翻译,获得代码:

    基础委托(Delegate)

    在.Net中声称委托行使重要词delegate,委托具有多样使用方法(以下均为同步委托调用):

     1     /// <summary>
     2     /// 普通委托基础调用方式(同步委托)
     3     /// </summary>
     4     public class Delegates
     5     {
     6         /// <summary>
     7         /// 定义有参无返回值委托
     8         /// </summary>
     9         /// <param name="i"></param>
    10         public delegate void NoReturnWithParameters(string o);
    11         /// <summary>
    12         /// 构造函数实例化
    13         /// </summary>
    14         public void DemoOne()
    15         {
    16             NoReturnWithParameters methord = new NoReturnWithParameters(this.Test);
    17             methord.Invoke("One-ok");
    18         }
    19         /// <summary>
    20         /// 赋值对象
    21         /// </summary>
    22         public void DemoTwo()
    23         {
    24             NoReturnWithParameters methord = this.Test;
    25             methord.Invoke("Two-ok");
    26         }
    27         /// <summary>
    28         /// DotNet 2.0 
    29         /// </summary>
    30         public void DemoThree()
    31         {
    32             NoReturnWithParameters methord = new NoReturnWithParameters(
    33                 delegate (string o)
    34                      {
    35                          Console.WriteLine("有参无返回值:{0}", o);
    36                      }
    37             );
    38             methord.Invoke("Three-ok");
    39         }
    40         /// <summary>
    41         /// DotNet 3.0 
    42         /// </summary>
    43         public void DemoFour()
    44         {
    45             NoReturnWithParameters methord = new NoReturnWithParameters(
    46                 (string o) =>
    47                     {
    48                         Console.WriteLine("有参无返回值:{0}", o);
    49                     }
    50             );
    51             methord.Invoke("Four-ok");
    52         }
    53         /// <summary>
    54         /// 委托约束
    55         /// </summary>
    56         public void DemoFive()
    57         {
    58             NoReturnWithParameters methord = new NoReturnWithParameters(
    59                 (o) =>
    60                 {
    61                     Console.WriteLine("有参无返回值:{0}", o);
    62                 }
    63             );
    64             methord.Invoke("Five-ok");
    65         }
    66         /// <summary>
    67         /// 方法只有一行去则掉大括号及分号
    68         /// </summary>
    69         public void DemoSix()
    70         {
    71             NoReturnWithParameters methord = new NoReturnWithParameters((o) => Console.WriteLine("有参无返回值:{0}", o));
    72             methord.Invoke("Six-ok");
    73         }
    74         public void DemoSeven()
    75         {
    76             NoReturnWithParameters methord = (o) => Console.WriteLine("有参无返回值:{0}", o);
    77             methord.Invoke("Seven-ok");
    78         }
    79         /// <summary>
    80         /// 定义有参无返回值测试方法
    81         /// </summary>
    82         /// <param name="o"></param>
    83         private void Test(string o)
    84         {
    85             Console.WriteLine("有参无返回值:{0}", o);
    86         }
    87         /*
    88          * 作者:Jonins
    89          * 出处:http://www.cnblogs.com/jonins/
    90          */
    91     }
    

     

      //一幅1036*1036字节数据定义
        public struct ScientificData
         {
            /参数信息
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
           public byte[] RelativePacketCount;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
           public Byte[] AbsolutePacketCount;
         ........
          public byte aux_3a;//填充3A H
           .........
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1036)]
            public OneImageRow[] ImageData;//图像数据行
    /// <summary>
    /// 获取raw图数据
    /// </summary>
    /// <returns>图像数据</returns>
    public ushort[,] GetImageData()
    {
    ushort[,] rawdata = new ushort[1036, 512];
    for (int i = 0; i < 1036; i++)
    {
    var onerow = ImageData[i];
    for (int j = 0; j < 512; j++)
    {
    rawdata[i, j] = (ushort)(((onerow.imagedata[j * 2] << 8) | onerow.imagedata[j * 2 + 1])) ;
    }
    }
    return rawdata;
    }
    }
    
    namespace ConsoleApp1
    {
      internal class Program
      {
        private static void Main(string[] args)
        {
          string first = "First";
          ((Action) (() => Console.WriteLine(first)))();
          ((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(first);
          ((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(first, "second");
        }
      }
    }
    

    协助进行委托&异步委托

    一齐委托:委托的Invoke艺术用来开展同盟调用。同步调用也足以叫阻塞调用,它将堵塞当前线程,然后推行调用,调用实现后再持续向下举办。

    异步委托:异步调用不封堵线程,而是把调用塞到线程池中,程序主线程或UI线程能够继续实行。委托的异步调用通过BeginInvokeEndInvoke来实现。

    以下为异步委托调用方式:

     1     class Program
     2     {
     3         /// <summary>
     4         /// 定义有参无返回值委托
     5         /// </summary>
     6         /// <param name="i"></param>
     7         public delegate void NoReturnWithParameters(string o);
     8         static void Main(string[] args)
     9         {
    10             NoReturnWithParameters methord = new NoReturnWithParameters(Test);
    11             Console.WriteLine("主线程执行1");
    12             Console.WriteLine("主线程执行2");
    13             methord.BeginInvoke("demo-ok", null, null);
    14             Console.WriteLine("主线程执行3");
    15             Console.WriteLine("主线程执行4");
    16             Console.ReadKey();
    17         }
    18         /// <summary>
    19         /// 异步调用委托方法
    20         /// </summary>
    21         /// <param name="o"></param>
    22         static void Test(string o)
    23         {
    24             Console.WriteLine("有参无返回值:{0}", o);
    25         }
    26         /*
    27          * 作者:Jonins
    28          * 出处:http://www.cnblogs.com/jonins/
    29          */
    30     }
    

    因为调用BeginInvoke为异步委托,不会堵塞主线程,运转结果如下:

    图片 1

     

    图像数据结构体如下:

    本文由金沙国际官网发布于编程,转载请注明出处:内存映射大文件并使用Marshal解析结构体信息

    关键词:

上一篇:没有了

下一篇:没有了