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

Preventing Multiple Application Instances..

Status
Not open for further replies.

dmuz

MIS
Aug 21, 2001
1
CA
We have a problem with a home grown Visual FoxPro application at our site where as many of the users will start multiple instances of the application up thinking they didn't have it running already... Causing problems on both the workstation and our databases...

So... How can I get Visual FoxPro 5.0\6.0 to check the Operating System to see if the application is already running and if so either display a message to the user that an instance of this application is already running and or just bring the requested application to the fore-front?

 
Hi dmuz,

I ran into a similar problem a few months ago, and was able to resolve it one of two ways. The simplier way was to use shared databases and tables, yet this does cause problems, you can't modify or pack the table.

The other was a piece of code i found somewhere on the net that checks if the instance is running, if it is, it shuts the new one its trying to open down (I turned it into small class). The piece of code uses (I'm not totally sure of the details, but it has to do with the mutex). If you would like a copy of this, you can contact me offline at shardie@kpcorp.com

Stephen
 
Hi dmuz,

A couple of ways to do this

1) When the app starts up, have it open a file on the local C: drive (or the user's network personal directory)exclusively, for instance with
Code:
On Error Do ProcedureToSayApplicationRunningAndQuit
create table (sys(2023) + "\applicationname") (A C(1))
that way, if they start a second copy, it will trigger the procedure to exit, because the second instance won't be able to create the file - which is locked by the first instance.

The drawback with this approach may be, if the application crashes, the file may remain locked until logout (althogh I've just tried it and it seems to cope with a crash okay)


2) A second way was included in a Readme (possibly about Foxtools) with FoxPro 2.x (if you've got a copy lying about!) that is, to use the WinAPI to look at the Titles of all the currently open applications - if you find the title of your App then you know it's running. Unfortunately I can't find a copy of it, but if nothing else, you could post on the FoxPro 2.6 forum and someone should be able to give you the code
 
This may be of use to you.
I adapted some code to get the handles of multiple versions of WINAMP running at the same time in order to get the file handle of the most recent version. I have removed a lot of my code and left the basic nuts and bolts.
Substitute the reference to WINAMP with the name of your app. If NOTO>0 then a version of your app. is already running

MGETWINTXT = REGFN("GetWindowText", "I@CI", "I")
MGETWINDOW = REGFN("GetWindow", "II", "I")

STUFFER=""
HWNDNEXT = CALLFN(MGETWINDOW,FOXHWND,0)
* Check any open windows are Winamps
DO WHILE HWNDNEXT <> 0
IF (HWNDNEXT <> FOXHWND) AND CALLFN(MGETWINDOW,HWNDNEXT,4) = 0
STUFFER = SPACE(64)
*Get the handle of the current window
X = CALLFN(MGETWINTXT,HWNDNEXT,@STUFFER,64)
* Is this window a Winamp?
NOTO=OCCURS(&quot;WINAMP&quot;,UPPER(STUFFER))
IF NOTO>0

* The current file is a version of WINAMP

ENDIF
ENDIF
HWNDNEXT = CALLFN(MGETWINDOW,HWNDNEXT,2)
ENDDO

 
faq184-839
thread184-36129
thread184-59381
thread184-95381 Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top