MemoryMappedFile::CreateNew Method (String^, Int64)
Creates a memory-mapped file that has the specified capacity in system memory.
Assembly: System.Core (in System.Core.dll)
Parameters
- mapName
-
Type:
System::String^
A name to assign to the memory-mapped file.
- capacity
-
Type:
System::Int64
The maximum size, in bytes, to allocate to the memory-mapped file.
Return Value
Type: System.IO.MemoryMappedFiles::MemoryMappedFile^A memory-mapped file that has the specified name and capacity.
| Exception | Condition |
|---|---|
| ArgumentException | mapName is an empty string. |
| ArgumentNullException | mapName is null. |
| ArgumentOutOfRangeException | capacity is less than or equal to zero. |
Use this method to create a memory-mapped file that is not persisted (that is, not associated with a file on disk), which you can use to share data between processes.
The following example is composed of three separate processes (console applications) that write Boolean values to a memory-mapped file. The following sequence of actions occur:
Process A creates the memory-mapped file and writes a value to it.
Process B opens the memory-mapped file and writes a value to it.
Process C opens the memory-mapped file and writes a value to it.
Process A reads and displays the values from the memory-mapped file.
After Process A is finished with the memory-mapped file, the file is immediately reclaimed by garbage collection.
To run this example, do the following:
Compile the applications and open three Command windows.
In the first Command window, run Process A.
In the second Command window, run Process B.
Return to Process A and press ENTER.
In the third Command window, run Process C.
Return to Process A and press ENTER.
The output of Process A is as follows:
Start Process B and press ENTER to continue. Start Process C and press ENTER to continue. Process A says: True Process B says: False Process C says: True
Process A
Process B
Process C
Available since 4.0