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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

See if a program is already running

Status
Not open for further replies.

LindaRichard

Programmer
Feb 23, 2001
103
CA
I use the following code to make sure that my program is not
already running when it is launched. The problem with this code is that it checks
the forms caption. I change the caption continuously
e.g. Radiogenic Isotope Database - U/Pb Data
or Radiogenic Isotope Database - Argon Data
Depending on what's being viewed. I can't figure
out how to check for Radiogenic Isotope Database in
the string. Any suggestions on how to get this to work
or an alternative method would also be fine.

Thanks
Linda Richard

*----------------------------------------------------------
* Check if a copy of this program is already running on this machine
*----------------------------------------------------------
*access the foxtools library, it contains certain required functions
SET LIBRARY TO FOXTOOLS
*find the current window
GetWind=RegFn("FindWindow","CC","I")
*get the application handle
apphand=CallFn(GetWind,0,"Radiogenic Isotope Database")
*if a handle is returned the program is already running
IF apphand<>0
*warn the user
MESSAGEBOX(&quot;This program is already running. Only one instance can run at a time.&quot;,16,&quot;Returning to WINDOWS.&quot;)
*an error has occured so end the program
End_The_Program() &&procedure located below
ENDIF
*remove the foxtools library
SET LIBRARY TO
 
Hi Linda,

I wrote something similar, which just checked the left 10 characters of the caption as follows:

I would imagine that in VFP7, you could make use of the hWND property, but I don't know how!

Hope it helps,

Stewart

Code:
LOCAL RetVal, hNext, cText, lnTextLen
RetVal = .f.
DECLARE INTEGER GetActiveWindow IN Win32API
DECLARE INTEGER GetWindow IN Win32API ;
	INTEGER hWnd, INTEGER nType
DECLARE INTEGER GetWindowText IN Win32API ;
	INTEGER hWnd, STRING @cText, INTEGER nBuffSize && returns title bar
DECLARE INTEGER BringWindowToTop IN Win32API ;
	INTEGER hWnd
hNext = GetActiveWindow()		&& get current top-most window
DO WHILE hNext<>0 
 cText = REPLICATE(CHR(0),80)
 lnTextLen = GetWindowText(hNext,@cText,80) && passing by reference
 IF lnTextLen > 0 AND LEFT(cText, 10) = [Win-I-Fred 6 Version]
  BringWindowtoTop(hNext)
  RetVal = .t.
  EXIT
 ENDIF
 hNext = GetWindow(hNext,2)
ENDDO
RETURN RetVal
 
I use this method:

STORE 0 TO chk, tryit
IF FILE('C:\MyProg.zzz')
tryit = FOPEN('C:\MyProg.zzz', 2)
IF tryit < 0
MESSAGEBOX(&quot;App already running&quot;, 16, &quot;Can't continue&quot;)
RETURN
ELSE
=FCLOSE(tryit)
ERASE C:\MyProg.zzz
ENDIF ( tryit < 0 )
ELSE
chk = FCREATE('c:\MyProg.zzz')
ENDIF

If the app can get exclusive use or create the file, the app isn't running. Otherwise, it is.

Dave S.
 
You could also use another technique.
In your main prg check if a specific table exist in the windows/temp directory, if it exists open it exclusively. If it does'nt create it. And when the app is being started a second time, trap the error:
Code:
ON ERROR myError
CD SYS(2023)
IF FILE(&quot;CHECK.DBF&quot;)
   USE CHECK.DBF EXCLUSIVE
ELSE
   CREATE TABLE CHECK (name (1))
    USE CHECK.DBF EXCLUSIVE
ENDIF
PROCEDURE myError
messagebox(&quot;The application is already running&quot;)
QUIT
ENDPROC

That way you don't need to depend on the title of the application


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top