Click to See Complete Forum and Search --> : Tricky: 2 apps share 1 variable
beamer-dreamer
October 25th, 2006, 02:00 PM
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!
wildfrog
October 25th, 2006, 02:33 PM
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
Craig Gemmill
October 25th, 2006, 05:27 PM
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/library/system.threading.mutex.aspx
Just make sure you read the full documentation, and remember to call .ReleaseMutex() / .Close() on any Mutex you use.
MikeAThon
October 25th, 2006, 07:48 PM
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/en-us/library/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
HanneSThEGreaT
October 26th, 2006, 01:17 AM
DDE
Just a small note:
DDE is no longer supported in VB.NET.
jhammer
October 26th, 2006, 03:03 AM
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.
beamer-dreamer
October 26th, 2006, 09:11 AM
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)... :) :) :)
beamer-dreamer
October 26th, 2006, 03:58 PM
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!
beamer-dreamer
October 29th, 2006, 02:25 PM
I tried the solution with a mutex and it works like a charm! :)
Thanks a lot guys for your help!!!
beamer-dreamer
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.