How to share values ​​in memory mapped files

I have a window form with datagridview and I am reading a specific column value in a list. I need to split all the values ​​of a list in a file with one memory, but below is my concern: 1. Finding the size of the list in bytes. 2. you need to separate all the elements of the list.

here is my example code where i am using one variable value:

  string MyName = "Seema";
  int totalBytes = MyName.Length * sizeof(Char) + 4;
  public List<string> myList = new List<string>();

MemoryMappedFile MyText = MemoryMappedFile.CreateOrOpen("MyGlobalData", howManyBytes);
                byte[] array1 = new byte[howManyBytes];
                array1 = GetBytes(Name);

                using (var accessor = MyText.CreateViewAccessor(0, array1.Length))
                {
                    accessor.WriteArray(0, array1, 0, array1.Length);
                }


 static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

      

suppose mylsit has elements 1. Apple 2. Mango 3. Pineapple

please tell how can I go with the above code.

+3


source to share


1 answer


You will need to use a mutex and want to store the size of the array as the first element in mmf.



0


source







All Articles