6

I need to pass a C# memory object from one process to another (IPC)

I've just tried to serialize this object in a file and then deserialize it in my 2nd process with binary serialization (BinaryFormatter) in order to have good performance.

Unfortunately, the performance is not up to what I expected. As my object has a lot of information, serialization and deserialization takes too much times (the serialization of my object takes more than 1MB on my hard drive).

I have heard of

Memory-mapped files (MMF)

which seems to be one of the fastest method for IPC when the objects to share between process are simple. What is the fastest and easiest way to communicate between 2 processes in C#?

My object is only simple nested struct like this :

public struct Library
{
    public Book[] books;
    public string name;
}

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

=> Is it possible to avoid serialization/deserialization and share this kind of object with MMF ?

=> What should be the characteristics of the shared object to avoid these operations of sérialization/deserialization ?

One more constraint : My first process is a C# 64 bits process and my 2nd process is a 32 bits one.

Thank you

Community
  • 1
  • 1
Patrice Pezillier
  • 4,476
  • 9
  • 40
  • 50

2 Answers2

4

You can't directly allocate objects in Memory Mapped File with C# (staying in safe code). So it means you need to have some sort of serialization to transfer of data between two applications.

Broad options:

  1. keep all raw data (more or less as byte array) in the MMF and have C# wrappers to read/write data on demand.
  2. Find faster serialization/build one by hand
  3. Use some form of change tracking and send only diffs between applications.

I'd personally go with option 3 as it will give the most reliable and type safe gains if applicable to particular problem.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Is there any kind of blog posts or article anyone knows of that would demonstrate something like this specifically as it relates to MMF and Binary Serialization? If someone comes across this one day - paste here! – DtechNet Jan 28 '19 at 02:15
0

Is it possible to avoid serialization/deserialization and share this kind of object with MMF ?

Use a var/foreach statement to iterate the elements of your Book[] items, and write them to a MMF by creating a view accessor.

Example :

 var BookWriter = (whatever you named Book[]);


Foreach(var in BookWriter)

{

(Using MMF...))
    {  
        (Using Accessor.MMF...))
        {      
        Accessor.write(point1, Bookwriter[0]);
        Accessor.write(point2, Bookwriter[1]);
         }//dispose ViewAcessor.
    }// disposes the MMF handle...`

}// Finished iterating Book[i]...
souki
  • 1,305
  • 4
  • 23
  • 39
Bo Jangles
  • 51
  • 3