I created a C# program which does some stuff. Let's call it Program B. I want to open Program B's executable in another program, Program A, I am creating also in C#. What I want to do is to get some information(actually a custom object) from Program B on a button click which also will close the program and add the information in a List in Program A. I want to use Memory Mapped Files for this, but somehow I didn't manage to do this until now. I have created a class:
public class MemoryMappedFileCommunicator{
public MemoryMappedFileCommunicator()
{
MemoryMappedFile = MemoryMappedFile.CreateOrOpen(MMF_FILE_NAME, MMF_MAX_SIZE);
// Create a stream which allows it to write data from offset 0 to 1024 (whole memory)
MemoryMappedViewStream = MemoryMappedFile.CreateViewStream(0, MMF_VIEW_SIZE);
_commandToSend = new Command();
//callback = new SendOrPostCallback(OnDataReceivedInternal);
//operation = AsyncOperationManager.CreateOperation(null);
}
}
I tried using SendOrPostCallback seen in a tutorial, but it didn't make sense to me. I also tried using an event handler, but my event did not fire. What I actually want to do is to fire an event every time I finished writing in the file, so Program A could read the information and put it into a List.
Write function looks like this:
public void WriteData(MyCustomObject obj)
{
FileStream writer = new FileStream(MMF_FILE_NAME, FileMode.Append);
DataContractSerializer ser = new(typeof(MyCustomObject ));
ser.WriteObject(writer, obj);
writer.Close();
DataWrittenEvent();
}
My delegate is void and has no parameters
Read function looks like this:
public MyCustomObject ReadData()
{
FileStream reader = new FileStream(MMF_FILE_NAME, FileMode.Open);
XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(reader, new XmlDictionaryReaderQuotas());
DataContractSerializer ser = new DataContractSerializer(typeof(MyCustomObject ));
MyCustomObject deserializedObj = (MyCustomObject )ser.ReadObject(xmlDictionaryReader, true);
xmlDictionaryReader.Close();
reader.Close();
return deserializedObj;
}
In every program I create an instance of MemoryMappedFileCommunicator, but only in Program A's constructor I attach the DataWrittenEvent event, but it is null all the time.
What am I doing wrong? How could I manage to do this?
I expected to make a callback from Program B to Program A like this: Program B: get information -> serialize information -> write serialized information to MMF (this should rise an event to Program A) Program A: read from MMF -> deserialize information -> add to list
So I figured it out finally. The way I work with the 2 processes is the following:
The solution looks like this:
Process A:
Process B: