Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Preventing application from running more than once
Message
De
03/09/1997 12:11:47
Bob Lucas
The WordWare Agency
Alberta, Canada
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00048262
Message ID:
00048307
Vues:
19
>>>I put code in my application that checks to see if it is already running and if it is it stops loading and informs the user with a messagebox that it is already running. I would like to modify this so if the user tries to run it a second time instead of getting the messagebox, the current running application is brought to the front and maximized. I am using VFP 5.0a.
>>>
>>>Any ideas?
>>>
>>>
>>>Jeff Rusch
>>>UW-Madison
>>
>>You could replace messagebox line in your code with API call ShowWindow (see in Win32Api Help).
>
>Hi Edward, thanks for the response. Where can I find Win32API help?
>
>Jeff


Here is some code snippet that I use to solve this problem. One of the unique difficulties I have is that the window title for the application also includes the Database name (SQL SERVER) that the user logged into. Therefore, I cannot check for a specific window title, but a title that contains a string.

It is MUCH MUCH easier to just check for a specific window title because then you can use findwindow with the window title.

*-- check to see if the app is already running!

cclassname = space(100)
lcTitle = 'Application Title'

DECLARE INTEGER GetClassName IN win32api INTEGER, STRING, INTEGER
DECLARE INTEGER GetWindowText IN win32api INTEGER, STRING, INTEGER
DECLARE INTEGER GetWindow IN win32api INTEGER, INTEGER
DECLARE INTEGER GetDesktopWindow IN win32api
DECLARE INTEGER FindWindow IN win32api STRING, STRING
DECLARE INTEGER IsIconic IN win32api INTEGER
DECLARE INTEGER GetWindowWord IN win32api INTEGER, INTEGER
DECLARE INTEGER GetParent IN win32api INTEGER
DECLARE ShowWindow IN win32api INTEGER, INTEGER
DECLARE SetForegroundWindow IN win32api INTEGER

*-- locate a running copy of the application
*-- NoCheck is a parameter passed to the app that forces a second copy to run even if one is already running

nWinHandle = IIF(lcNoCheck = 'NoCheck', 0, FindActiveApp())

IF nWinHandle # 0 && Application Already Exists
WAIT WINDOW "Activating currently Running copy of the application" TIMEOUT 0.5

IF IsIconic(nWinHandle) = 1 && Window is minimized.
ShowWindow(nWinHandle,4) && Restore minimized window to foreground.
ENDIF

SetForegroundWindow(nWinHandle) && Make existing window active
.
ELSE
*-- do normal stuff here
ENDIF



FUNCTION FindActiveApp
*-- this function checks to see if a current copy of this application
*-- is running and will activate that copy if it is
*-- Returns the window handle if found, 0 otherwise

*-- the search is for a EXE version of the application ('Fox_100000001')

LOCAL nretval, nHandle, cwintitle
nretval = 0
nHandle = FindWindow("Fox_100000001",NULL)

DO WHILE nHandle # 0
nHandle = GetWindow(nHandle, 2)
cwintitle = space(100)
x = GetWindowText(nHandle , @cwintitle, 100)

IF lcTitle $ cwintitle
nretval = nHandle
EXIT
ENDIF

ENDDO

RETURN nretval
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform