Search code examples
c#callbackstreamshared-memorymemory-mapped-files

Memory Mapped Stream - Wait for data to arrive


I'm trying to make application A write an byte array into a Memory Mapped Stream, application B then reads this piece of memory. I've got to the point where they can read and write but the problem is I can't get application B to wait for new data to arrive, right now it just reads as fast as possible.

After searching around for a bit I found out that you could assign a callback to MemoryMappedStream.BeginRead but i'm unsure how to use this callback to wait for new data from the stream. How would one make a callback for this kind of situation? Or am I looking into the wrong direction?


Solution

  • Thanks to MatthewWatson I looked into EvenWaitHandles and managed to make the processen wait upon each other's signal with the use of two AutoResetEvents. One of them is used to set MemoryReadEventWaitHandle when the reading is performed and the other sets MemoryWrittenEventWaitHandle when the writing is done. This way they will not interfere with one another and perform the tasks in correct order.