Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One instance of an application allowed open 1

Status
Not open for further replies.

kindred

Programmer
Feb 28, 2001
50
US
Is there a way to make sure that multiple instances of the application do not open per machine?

I saw one post awhile back explaining one way of doing this, but seemed like 3 pages of code just to check for this. I just wanted to find out if there's a way to do this in a simple fashion.


Thanks,
Stephen Hardie
 
Hi Stephen,

I don't know how to do it directly, but you could create a little "flag" file that would contain one value when the application was running and another when not. The 1st thing the app would do would be to check this file to see if it could continue.

Jim
 
Thanks for the tip, but unfortunately, this is what I'm using right now and on the network, it isn't 100% accurate. I write a blank file out to disk like started.txt, and then delete it on quit. I need more of a robust method of doing it.

Stephen
 
Hi Stephen,

Check out the following code:

PROCEDURE chk_active
*-------------------------------------
*- SE: Wietze Veld
*- DateTime: 11/25/00 02:03:11 PM
*-------------------------------------
* Check if the application is already active.
LOCAL llRetVal
DECLARE INTEGER FindWindow IN Win32Api INTEGER, STRING
DECLARE INTEGER SetForegroundWindow IN win32api INTEGER
NullPointer = 0

HWND = FindWindow(NullPointer, APP_NAME)
llRetVal = (HWND > 0)
IF llRetVal
=SetForegroundWindow(HWND)
ENDIF
RETURN llRetVal

ENDPROC

APP_NAME is a named constant that stores my application name:
f.i. "My application", which is the title of my main window.

Disadvantage is that the title cannot be changed during runtime.

HTH,
Weedz (Wietze Veld)
veld4663@exact.nl

They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best.

After the GoldRush - Steve McConnell
 
Hi Stephen,

I'll presume the mysterious 3 pages of code you speak of is from my post in: thread184-36129
If for whatever reason you choose not to use it, it's your perogative. But I will say, its very robust and works like a charm.

The following are a few additional methods I've seen used, but I would advocate against:

1. Creating a file upon entry. Deleting upon exit.
2. Setting a field .T. upon entry. Setting .F. upon exit.

The reason I advocate not using these methods, is precisely as you mentioned...it's not 100% accurate in the event your app crashes and isnt allowed to perform its exit method.

The following are a few methods I've seen used, that I feel are adequate for this purpose.

1. Locking a certain record in a table on entry. For example, the users record in a user table.
2. Opening a table exclusively on entry.

In the event your app crashes, the resources are freed. Jon Hawkins

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Thanks for the help everyone. Its very much appreciated.

Jon,

I considered using yours, but for myself, i like understanding every piece of code i use or write just incase i ever have to go back. Im not saying yours isnt difficult to understand, but it is quiet complex, and would take myself sometime to fully understand what its doing.

As for the checking if local dbf prefs file is in use, i've considered this as one option, just didnt want to go in that direction, having to have a local exclusive dbf everytime isnt ideal for me.

Im going to try the FindWindow and see what happens.


Thanks again everyone.
 
Just tried Weedz piece of code and it continued to open multiple instances of the form, here's my whole startup.prg file, maybe someone can explain to me what I'm doing wrong here?

PUBLIC GDefault

*set step on

IF Application.StartMode = 0
GDefault = sys(5) + sys(2003)
ELSE
GDefault = Substr(Application.Servername, 1, Rat('\', Application.Servername)-1)
ENDIF

DECLARE INTEGER FindWindow IN Win32Api INTEGER, STRING
DECLARE INTEGER SetForegroundWindow IN win32api INTEGER
MNullPointer = 0
MPosition = FindWindow(MNullPointer, 'Sample')
MActive = (MPosition > 0)
IF MActive = .F.
* =SetForegroundWindow(MPosition)
ELSE
DO Form(GDefault + '\Forms\main.scx')
READ EVENTS
ENDIF
 
HI Kindred,

My piece of code only returns .T. or .F. indicating if another instance with the same window caption is already open. The only thing it does is checking for another instance. Its up to you to prevent continuing starting of your application when this function returns .T.

So if an application with the window caption "My application" is already open and the application you are trying to start has the same caption, the function will return .T.

In essence this fucntion does not prevent your application from starting when another instance is already active.

It merely indicates another instance is already active.

HTH,
Weedz (Wietze Veld)
veld4663@exact.nl

They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best.

After the GoldRush - Steve McConnell
 
Here is my startup code to give you an example:

*-------------------------------------
*- SE: Wietze Veld
*- DateTime: 10/14/00 04:29:30 PM
*-------------------------------------
*- Main procedure to start the application
LPARAMETER tcDevelopment
#INCLUDE APP.h
LOCAL lcDevelopment, lcPath
PUBLIC CB_APP
lcDevelopment = IIF(VARTYPE(tcDevelopment) == 'C', tcDevelopment, SPACE(0))

ON ERROR
lcPath = SET('PATH') + "," + CURDIR() + "GRAPHICS"
SET PATH TO &lcPath

SET SYSMEN TO
SET PROCEDURE TO CB_INIT ADDITIVE
Set_ClassLibs()

IF !chk_active()
CB_APP = CREATEOBJECT('cstcb_app')
CB_APP.dev_mode = (lcDevelopment == "/1")
CB_APP.Start_up()
READ EVENTS
ELSE
QUIT
ENDIF Weedz (Wietze Veld)
veld4663@exact.nl

They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best.

After the GoldRush - Steve McConnell
 
Thanks everyone,

Before I saw weedz post, i went and tried Jon's piece of code he referenced me too, and it worked perfectly.


Stephen

 
Stephen,

if there's anything you dont understand or would just like further explanation about any part of the code, just let me know.

jonscott8@yahoo.com Jon Hawkins

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top