locked
memory mapped file - request for 4 code samples RRS feed

  • Question

  • User-1982615125 posted

    in order to understand the usage of memory mapped files, i would like someone to to create 4 codes for me. preferably vb.net but c# will do fine too.

    1. Code to create a memory mapped file of plain text from a txt file
    2. Code to acces that memory mapped file from another process and response.write that text
    3. Code to set the text on the memory mapped file to something different
    4. Code to save the memory mapped file back to disk (i guess i can just filesystem.writealltext({the text from point 2}))

    i dont need any explenation, the code will speak for itself, as long as its as minimalistic as possible, so it 'just' works. if anyone can do this for me, it will be the best example on the internet. and you will get the accepted answer mark.

    keep in mind that there should be some kind of file lock to prevent corruption when multiple asp instances acces the file.

    thank you!

    Saturday, June 9, 2018 11:32 AM

All replies

  • User475983607 posted

    Simply read the the openly published documentation.

    https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files

    Saturday, June 9, 2018 11:46 AM
  • User-1982615125 posted

    i could have pasted a link myself.

    if you can do as requested, i will mark it as the answer.

    Monday, June 11, 2018 7:30 PM
  • User475983607 posted

    i could have pasted a link myself.

    if you can do as requested, i will mark it as the answer.

    Confused, the link has all the source code you requested in both C# and VB.NET versions.  Did you actually read the linked content?  If you did and are still having trouble, please post your code and explain the issue. 

    Monday, June 11, 2018 7:39 PM
  • User-1982615125 posted

    Here are several examples of what i can make up from the link. as you can see an infinite amount of things are wrong about my understanding of the examples.

    problems are:

    • I have to keep the file open by spilling time using an loop. i think this is not how its meant to be.
    • im not able to send more than 1 byte. either 0 or 1. but i need to place strings over 10 MB in size into the file eventually. so the current example using strings, doesnt seem to work
    • The code example shows it as a message system. but thats not how i intend to use it. so i revamped it back to a file design. but i dont know how it works, and neither do i know if this is capable of storing data, or is it just a stream, and all data is gone once its received?
    • is this faster than sending the data over named pipes?

    so please rewrite the entire thing.

         Sub CreateFile()
            Using mmf As MemoryMappedFile = MemoryMappedFile.CreateNew("testmap", 10000)
                Do
                    'wait forever to keep the file open
                Loop
            End Using
        End Sub
    
    
        Public Sub WriteToFile()
            Using mmf As IO.MemoryMappedFiles.MemoryMappedFile = IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting("testmap")
                Dim mTex As Threading.Mutex = Threading.Mutex.OpenExisting("testmapmutex")
                mTex.WaitOne()
                Using Stream As IO.MemoryMappedFiles.MemoryMappedViewStream = mmf.CreateViewStream(1, 0)
                    Dim writer As IO.BinaryWriter = New IO.BinaryWriter(Stream)
                    writer.Write("this is testcontent")
                End Using
                mTex.ReleaseMutex()
            End Using
        End Sub
    
        Public Sub ReadFromFile()
            Using mmf As IO.MemoryMappedFiles.MemoryMappedFile = IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting("testmap")
                Dim mTex As Threading.Mutex = Threading.Mutex.OpenExisting("testmapmutex")
                mTex.WaitOne()
                Using Stream As IO.MemoryMappedFiles.MemoryMappedViewStream = mmf.CreateViewStream(1, 0)
                    Dim reader As BinaryReader = New BinaryReader(Stream)
                    Console.WriteLine("filecontent=" & reader.ReadString())
                End Using
                mTex.ReleaseMutex()
            End Using
        End Sub

    Tuesday, June 12, 2018 1:10 PM
  • User475983607 posted

    I have to keep the file open by spilling time using an loop. i think this is not how its meant to be.

    I don't understand this comment.

    im not able to send more than 1 byte. either 0 or 1. but i need to place strings over 10 MB in size into the file eventually. so the current example using strings, doesnt seem to work

    Did you read the Managing Memory-Mapped Files link? It explains how the file memory maps technically works.  It is up to you - the programmer - to manage memory and know where your data begins and ends.  This is very low level stuff and requires a solid understanding of low level programming and managing menory.  This type of knowledge is found in Computer and Electrical Engineering.  Langues like Assembly, C, or embedded languages that are close to the metal also expose you to this type of programming.  An ASP.NET forum is not the right place to learn this stuff.

    The code example shows it as a message system. but thats not how i intend to use it. so i revamped it back to a file design. but i dont know how it works, and neither do i know if this is capable of storing data, or is it just a stream, and all data is gone once its received?

    The examples show how to load a file into a memory map, share, and manipulate the memory and ultimately the file.   I assume sorting is a function implemented outside the file memory map unless you need the file sorted in a particular order.  Maybe by a key field.  Let's say your records are 100 bytes.  Read 100 byte chunks and convert the chunks into an object collection.  This is concept is covered in the first code example.  Then sort the objects as you like using linq or the array type.

    is this faster than sending the data over named pipes?

    Not an answerable question as you are comparing apples and oranges.

    so please rewrite the entire thing.

    I don't understand the question. 

    I don't think this is a subject that can be learned on a website support forum.   It seems you are trying to build a database service which is far out-of-scope for a website support forum.

    Tuesday, June 12, 2018 1:39 PM
  • User-1982615125 posted

    i think i now found out, that it uses the pagefile on disk. instead of memory. is that right?

    because in this case, it wont be faster than named pipes at all.

    the very fastest thing is just to save a file to disk myself, and read it out when a FilesystemWatcher notices a chance. i really cant believe there is no better solution to interprocess communication. when you think about it, its depressing.

    what do i do now........ i dont want to settle for anything less than whats in my mind.

    Tuesday, June 12, 2018 4:25 PM