CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2006
    Posts
    83

    Question Tricky: 2 apps share 1 variable

    Here's a tricky problem:

    I am programming 2 applications that run seperately from one another. But from time to time they have to pass a piece of information to the other app. This info is of the type INT32.

    What I tried is using a database and write this number into it, but I keep on having problems. Maybe that occurs when both apps try to write at the same time.

    Is there another way, maybe right within the RAM of the PC where I could store this info. This would be a lot faster than my harddisk with the database on it and thereby reduce the chance of both writing into that space at the same time. The app that reads the data puts a '0' in the DB to show that it has received the data; now before one app tries to put another piece of info there, it checks if the space is "empty" i.e. if there is a '0'. But if the other app also checks for '0' in the small time between the first app checked and then writes its data it may get overwritten by the data from the other app, that also found a '0'. That's why cutting down the time between checking and writing might be critical.

    Thanks for any ideas!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Tricky: 2 apps share 1 variable

    That's why cutting down the time between checking and writing might be critical.
    No matter how much you cut that time down there will always be a small timeslice where this error may resurect. It's called a race condition and it's a problem even if you switch to file based or memory based storage.

    To solve this, from a database perspective, you would use transactions (with the proper isolation level?). Another option is to synchronize the applications using a Mutex or other synchronizing object, but IMO it the databases responsibility to maintain the integrity of its data.

    - petter

  3. #3
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Tricky: 2 apps share 1 variable

    Compared to other languages, it's almost trivial to use MUTEXes in .NET these days.

    I'm onboard for the Mutex solution as well:
    http://msdn2.microsoft.com/en-us/lib...ing.mutex.aspx

    Just make sure you read the full documentation, and remember to call .ReleaseMutex() / .Close() on any Mutex you use.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Tricky: 2 apps share 1 variable

    Mutexes are good for synchronization, but I think that the problem faced by the OP is more along the lines of "how do I transfer data" rather than "how do I synchronize the transfer of data when I have already selected the transfer mechanism".

    What you are looking for is called "Interprocess Communications". See "Interprocess Communications" at http://windowssdk.msdn.microsoft.com.../ms690478.aspx , which serves as a jumping-off point for describing the following IPC mechanisms:

    Clipboard
    COM
    Data Copy (i.e., WM_COPYDATA)
    DDE
    File Mapping
    Mailslots
    Pipes
    RPC
    Windows Sockets

    Storing data to a database, as you are doing now, seems to be clumsy, particularly since you're only transferring an INT32, even though it's true that it will work if properly synchronized (e.g. mutex).

    If your two apps will always be on the same machine, and since you are only trying to pass an INT32, then I would tend to recommend use of WM_COPYDATA, since it has the benefit of an inherent notification to the recipient (i.e., it's a WM message) that avoids many of the synchronization issues that you might otherwise encounter.

    Mike

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Tricky: 2 apps share 1 variable

    Quote Originally Posted by MikeAThon
    DDE
    Just a small note:
    DDE is no longer supported in VB.NET.

  6. #6
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Tricky: 2 apps share 1 variable

    One of the technologies to transfer data between applications is called .NET remoting. It supports IPC channels in 2005.
    The architecture is as follows: You do not connect to the database from the clients directly. You set up a server with several classes: The data access class - a class that connects to the database and gets and stores data, an interface class, that has the methods the clients can call, and a business object class, that has the data to share between the applications - in your case a single integer.

  7. #7
    Join Date
    Aug 2006
    Posts
    83

    Lightbulb Re: Tricky: 2 apps share 1 variable

    Thank you all, guys!!!

    I didn't expect so many responses and also so many different approaches. You guys rock!!!

    I'll look into the stuff you wrote, but it will take me a while to figure out what works best. I'll keep you posted how it went.

    In the meantime I have an other problem you might like to work on (see separate post)...

  8. #8
    Join Date
    Aug 2006
    Posts
    83

    Question Re: Tricky: 2 apps share 1 variable

    OK, I like the Mutex idea to synchronize the two applications. From what I've found searching the internet it sounds great. But I am a bit lost when it comes to how to actually implement that.

    What I would like to do is basically:

    1. Make the necessary declarations (DIM MyMutex AS...)
    2. Check if the other app has already created a mutex and create one if it doesn't exist yet.
    3. Wait for 30 seconds to get control of the mutex.
    4. Get control of the mutex.
    5. Release control of the mutex
    6. repeat from 3.

    I'd very much appreciate some code snippets here.

    In addition I couldn't find any info about how the two apps "know" that they are referring to the SAME mutex. I read that there's a possibility to give the mutex a name (string). How does that work?

    Thanks in advance!

  9. #9
    Join Date
    Aug 2006
    Posts
    83

    Re: Tricky: 2 apps share 1 variable

    I tried the solution with a mutex and it works like a charm!

    Thanks a lot guys for your help!!!
    beamer-dreamer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured