MemoryMappedFile, persistance and @Could not find a part of the path@ error

  • Thread starter Thread starter want 2 Learn
  • Start date Start date
W

want 2 Learn

Guest
I created 2 class 1 that send to the file and one that reads, I am using the persistent option.

1. on the MemoryMappedFile.OpenExisting I get error that "Could not find a part of the path"

not clear why it happends?

2. when I read the data out it's actually erased? or if I run the process again the items will be read again?

my target is pass data between 2 process.

this is the class that send data to the file:

private const int MMF_MAX_SIZE = 1024; // allocated memory for this memory mapped file (bytes)
private const int MMF_VIEW_SIZE = 1024*10; // how many bytes of the allocated memory can this process access
private readonly string filePath;
private readonly Thread thread;

public Sender(string filePath)
{
this.filePath = filePath;
thread=new Thread(Send);
thread.Start();
}

public void Send()
{
ManageQueue manageQueue = ManageQueue.Instance;
MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filePath, FileMode.OpenOrCreate, null,MMF_VIEW_SIZE ,MemoryMappedFileAccess.ReadWrite);
// creates a stream for this process, which allows it to write data from offset 0 to 1024 (whole memory)
MemoryMappedViewStream mmvStream = mmf.CreateViewStream(0, MMF_VIEW_SIZE);
SmsMessage smsMessage = null;
while (true)
{
while (manageQueue.incomingMessages.IsEmpty) ;
if (manageQueue.incomingMessages.TryDequeue(out smsMessage))
{
// serialize the variable 'message1' and write it to the memory mapped file
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(mmvStream, smsMessage);
mmvStream.Seek(0, SeekOrigin.Begin); // sets the current position back to the beginning of the stream
}
}//while(true)
}//Send() // the memory mapped file lives as long as this process is running
}

this is the reader class :

private const int MMF_MAX_SIZE = 1024; // allocated memory for this memory mapped file (bytes)
private const int MMF_VIEW_SIZE = 1024; // how many bytes of the allocated memory can this process access
private readonly string filePath;
private readonly Thread thread;

public Listener(string filePath)
{
this.filePath = filePath;
thread = new Thread(Receiver);
thread.Start();
}
public void Receiver()
{

// creates the memory mapped file
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(filePath,MemoryMappedFileRights.Read);
MemoryMappedViewStream mmvStream = mmf.CreateViewStream(0, MMF_VIEW_SIZE); // stream used to read data
BinaryFormatter formatter = new BinaryFormatter();
byte[] buffer = new byte[MMF_VIEW_SIZE];
SmsMessage smsMessage;
while (true)
{
// reads every second what's in the shared memory
while (mmvStream.CanRead)
{
// stores everything into this buffer
mmvStream.Read(buffer, 0, MMF_VIEW_SIZE);

// deserializes the buffer & prints the message
smsMessage = (SmsMessage)formatter.Deserialize(new MemoryStream(buffer));
Console.WriteLine(smsMessage.Text + "\n" + smsMessage.CreateDateTime + "\n");
//System.Threading.Thread.Sleep(1000);
}
Thread.Sleep(1000);
}//while (true)
}

thanks

Continue reading...
 
Back
Top