Click here to Skip to main content
14,304,866 members

Using FileMapping on .NET as IPC

Rate this:
3.00 (9 votes)
Please Sign up or sign in to vote.
3.00 (9 votes)
21 Jan 2007
Sample code for using FileMapping on .NET.


.NET inter-process mechanisms are very slow. With some system calls, it becomes easy to use file mapping and that's the faster mechanism for IPC.


  • MemMap

    This class encapsulates a file mapping zone with system page-file support. All you need to do is give it a name and size.


    'Process 1
    dim mem1 as MemMap=new MemMap("ZONE1",2048) 
    mem1.writeString(0,"String to share")
    'Process 2
    dim mem1 as MemMap=new MemMap("ZONE1",2048) 
    dim sharedString=mem1.readString(0)
    mem1.close() 'at the end of both programs

    This class handles the creation or reuse of file mapping within the "new" method and has methods for reading and writing integers and strings on a given offset.

    It gives also a locking function based on InterLockedExchanged with protection against process elimination while having holding a lock.


  • MemChannel

    This class uses the MemMap class to implement a FIFO queue of strings. You can create any number of channels by simply giving different names to each one of them.


    dim cha1 as MemChannel=new MemChannel("CHANAME1") 
    cha1.putMsg("This is the message")
    dim cha1 as MemChannel=new MemChannel("CHANAME1") 
    msgbox cha1.getMsg()
    cha1.close() 'at the end of both programs


  • MServer.exe

    Example of a server reading from a channel.

  • Client.exe

    Example of a client writing to a channel.

Points of interest

The sample programs included here runs at 250.000 msgs per second on a PIV 3 GHz. But they block each other while accessing the channel and the CPU is not 100% busy.

With MemMap class support, it becomes easy to implement other IPC mechanisms as shared Dictionaries.


  • 25-06-2005 - First version.
  • 20-01-2007 - Second version


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Spain Spain
No Biography provided

Comments and Discussions

Generalwith read from C++ program Pin
ftaka29-Apr-08 21:06
memberftaka29-Apr-08 21:06 
GeneralCode corrected and revised Pin
aalday21-Jan-07 20:18
memberaalday21-Jan-07 20:18 
GeneralSomething wrong Pin
Alexey_i21-Oct-06 1:33
memberAlexey_i21-Oct-06 1:33 
GeneralRe: Something wrong Pin
aalday21-Jan-07 20:16
memberaalday21-Jan-07 20:16 
GeneralFaster implementation Pin
studio_ukc28-Sep-06 8:18
memberstudio_ukc28-Sep-06 8:18 
QuestionCopyright/Usage? Pin
brian_birtle6-Nov-05 6:25
memberbrian_birtle6-Nov-05 6:25 
AnswerRe: Copyright/Usage? Pin
aalday6-Nov-05 9:29
memberaalday6-Nov-05 9:29 
GeneralNext time post in proper category Pin
fwsouthern25-Jun-05 19:51
memberfwsouthern25-Jun-05 19:51 
GeneralRe: Next time post in proper category Pin
aalday25-Jun-05 21:28
memberaalday25-Jun-05 21:28 
GeneralFORTRAN Pin
Anonymous25-Jun-05 13:23
memberAnonymous25-Jun-05 13:23 
GeneralSeems great Pin
Daniel Turini25-Jun-05 12:26
memberDaniel Turini25-Jun-05 12:26 
GeneralRe: Seems great Pin
aalday25-Jun-05 21:40
memberaalday25-Jun-05 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 25 Jun 2005


25 bookmarked