none
Need to share a parameter across several programs in C# RRS feed

  • Question

  • I need to share and increment a counter accross several different instances of a program - written in c#. I used to use named shared memory in a previous life, but cant seem to figure out how its currently done in c#. Can anyone help with a snipet?

    Thanks in advance,

    Austin

    Thursday, September 23, 2010 7:22 PM

Answers

  • > a counter

    Do you really care about the actual value of the counter or are you just using it as a semaphore for synchronization?

    The reason I am saying this is that there is a Semaphore class which can be used cross-process.

    You would need a "named semaphore".

    http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

    > I used to use named shared memory in a previous life

    .NET 4.0 has a FileMapping class.  It has a non-persisted variety -- that is what you want for this kind of shared memory.  You'll also need synchronization -- probably a named Mutex, to ensure that only one process accesses the shared memory concurrently and so that the value read is the last value written.

    http://msdn.microsoft.com/en-us/library/dd997372.aspx

     

     

    • Marked as answer by SamAgain Friday, October 1, 2010 10:00 AM
    Friday, September 24, 2010 1:46 AM