Preventing Multiple Instances - Using Sendmessage And Onmessage?


RodSwan

Recommended Posts

Is there any "simple" way to prevent multiple instances of daqfactory (silently!)?

I have problem with operators sometimes creating multiple instances. I would like any starts of Daqfactory to quietly exit if there is already an instance running (i.e. no "Click on OK to continue" dialogs).

First thought is to use a registry variable to check a global flag when Daqfactory starts, if set then exit, if not then set it and continue. When running instance exits it can clear flag.

Problem with this is if Daqfactory exits without clearing flag (i.e. power reset for example where OnShutdown() doesn't get time to execute) then system will not restart.

I've tried using the SendMessage with AddListener to try and send message to other instance if one exists (and then to get a message back to say GoAway!), but despite documentation saying this is Global it only works within an instance, not between multiple instances.

Any suggestions?

As an aside, I recently had an instance where clicking on a desktop shortcut would always start two instances. I always had to find and kill the second instance. When I rebuilt the machine (disk drive died) that problem went away.

This is all on Windows XP SP3 using DaqFactory 5.87 build 1969.

Rod.

Link to comment
Share on other sites

SendMessage / AddListener won't work. That's for passing messages between screen components and is a DAQFactory only thing. You could use the Windows function ::FindWindow() to try and find another DAQFactory window and if it exists, quit, but you'd have to know how to call that function (it requires extern() to pull the function into DAQFactory). That is actually the most reliable way. The easiest way is what you described, using a simple DAQFactory registry variable. The issue is also as you describe, that if DAQFactory exits without clearing the flag, you are in trouble. However, you can get around this by using a timestamp instead of a simple flag. Have your application update that registry variable to the current system time every 15 seconds or so. When the application starts, have it look at that variable and if the time there is more than 15 seconds old, its ok to start, otherwise quit because another instance must be running. The only problem would be if you quit and tried restarting within 15 seconds, but then you just try again and for sure 15 seconds will have passed. The other problem is at DST or other big clock changes, but DST is only twice a year and at 2am (at least in the states).

Link to comment
Share on other sites

Ok Thanks for the reply...

I like the idea of the Time stamp... I was concerned about witing to the registry that often. I must admit I have been putting more and more current state information into the registry so any restarts required can continue where the previous instance left off but writing once every 15 seconds would be a lot more frequent.... You're suggesting there should be no problems with that?

Rod.

Link to comment
Share on other sites

The registry is just a file managed by windows. Writing to it is pretty darn fast. If you have lots of parameters you want to update every 15 seconds, you might consider putting them all in a class, then every 15 seconds use toJson and the file. functions to update the file. The biggest problem with this is that, unlike the registry, if DF dies (say a power outage) in the middle of the write, you'll corrupt that file. Not a big deal when you are updating every hour because your exposure is much smaller. On way around this is to write to two alternating files.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.