选择,从内存中填充数据

时间:2018-05-19 17:21:18

标签: c# memory struct

我正在尝试使用读取内存填充对象。我正在使用结构,它的速度非常快,至少看起来像它。

float x = 0;

while (true)
{
    st.Start();

    List<DataChild> DataChildList = new List<DataChild>();
    var Data = Algorithm.Data;
    Data.ChildData.pData = IntPtr.Add(Data.ChildData.pData, 0x20);

    for (int i = 0; i < Data.NumberOfObjects; i++)
    {
        ChildData childData = Data.ChildDatas.ReadValue(i, true);

        if (childData.Render.X != x)
        {
            Console.WriteLine("Change detected");
            x = childData.Render.X;
                            }
            DataChildList.Add(childData)
        }

        Algorithm.Data.Datas = DataChildList;
        st.Stop();
        Console.WriteLine($"populated in {st.Elapsed.TotalMilliseconds} ms");

        st.Reset();
   }
}

有我的结构:

[StructLayout(LayoutKind.Explicit)]
public struct ChildData
{
  [FieldOffset(0x0)]
  public IntPtr BasePointer;

  [FieldOffset(0x400)]
  public IntPtr pRender;

  public Vector3 Render
  {
    get
    {
     return M.Read<Render>(this.pRender);
    }
  }
}

[StructLayout(LayoutKind.Explicit)]
public struct Render
{
 [FieldOffset(0x30)]
 public float X;

public float posX
{
  get
  {
   return this.X;
  }
}
}

StopWatch说我填充了0.005 ms.

中的数据

当我按下某个键时,它会更改childData.Render.X的值(目前列表中唯一的值)。

但它显示在0.5到1秒之间,与循环速度相比,这似乎非常慢。

不得不说这段代码正在运行一个线程。

关于为什么需要这么长时间的任何想法?谢谢!

0 个答案:

没有答案
相关问题