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

Stop EXE file from starting more than once 1

Status
Not open for further replies.

bebbo

Programmer
Dec 5, 2000
621
0
0
GB
Is there some way I can stop people from starting an application more than once ???
 
Hi,

There are probably better ways, but I create a file
on the HDD and keep it open using this code:
Code:
** try to create or open a file on the C: drive
M.FILEHANDLE = FCREATE("C:\"+SYSTEMNAME+".HDL")
IF m.FILEHANDLE < 0  && Check for error opening file
	= MESSAGEBOX(&quot;Unable to Access &quot;+SYSTEMNAME+m.CRLF+&quot;It is Probably Already Loaded&quot;+m.CRLF+m.CRLF+&quot;Look on Your Taskbar&quot;,0,&quot;Error&quot;)
	QUIT
ENDIF

where SYSTEMNAME is a character variable for the application in question.

The second time an app tries to create the file it can't
so the message pops up.

HTH
Regards

Griff
Keep [Smile]ing
 
beddo

To add to GriffMG's suggestion, take a look at faq184-1767 and faq184-1998.
Mike Gagnon
 
Yes I thought about doing something like that. What happens if the program is closed or crashes without closing it the proper way. The file would still be there.
 
Bebbo

You are right the file would be there, but the OS should release the lock - so the next instance can overwrite it.

You can add a 'clean-up' routine for the 'planned exit'
but I generally leave the file in place.

HTH Regards

Griff
Keep [Smile]ing
 
Assuming you mean you want only one instance running on one computer - put the following code in the start up .prg before anything else where 'MyWindowName' is the title bar of your programme window:

DECLARE INTEGER GetActiveWindow ;
IN Win32API
DECLARE INTEGER GetWindow IN Win32API ;
INTEGER hWnd, INTEGER nType
DECLARE INTEGER GetWindowText IN Win32API ;
INTEGER hWnd, STRING @cText, INTEGER nType
DECLARE INTEGER BringWindowToTop IN Win32API ;
INTEGER hWnd
DECLARE integer ShowWindow in win32api integer i1, integer i2

** iterate through all the open windows**
hNext = GetActiveWindow()
DO WHILE hNext<>0
cText = REPLICATE(CHR(0),80)
** get window title

GetWindowText(hNext,@cText,80)
IF 'MyWindowName'$cText
** programme is already running
** show existing instance maximised**
=ShowWindow(hNext,4)
=BringWindowToTop(hNext)
** Quit this instance of the programme**
QUIT
ENDIF
** try the next window

hNext = GetWindow(hNext,2)
ENDDO

Release all extended

**If you get this far there are no other instances running **so start it up
 
johnbuckley

this post looks just like the posted faq184-1998

bebbo
when using the winapi to check on program already running it is the first thing that is done before opening any files. then if it is found to be open then it is brought to the front. I have used the faq184-1998 on all my exe's Attitude is Everything
 
A comment concerning the method described above
using the API getWindow

I have tried this in the pass and the only way I could get
it to work was to

1. set the _screen.visible property to .t. before running the routine
otherwise hNext = GetActiveWindow() is set to hNext=0
and the do loop is not executed.
This is a little anoying because the screen must be made visible before I am
ready to show it.

2. Don't set _screen.caption property before running this routine or the program
will not run.

Anyone have any suggestions on how to avoid making the screen visible and still
getting this to work?

Linda
 
LindaRichard

Looks like a lot of us use similar code. To avoid your problem I use:

1. A config.fpw containing SCREEN = OFF
2. A startup prg with the above code as the first lines
3. Next in the prg a line:

_screen.visible=.f.

4. In the init of the first form
_screen.visible=.t.

Works for me
 
HI,
Look in my FAQ on this. Hope this helps :)

How to ensure that only one instance of my application is running in my desktop.
faq184-839 ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Hi,
I have a function I have got from another FoxMan:
***
FUNCTION FirstInstance(pMutex)
#DEFINE ERROR_ALREADY_EXISTS 183
DECLARE INTEGER CreateMutex IN WIN32API INTEGER, INTEGER, STRING @
DECLARE INTEGER CloseHandle IN WIN32API INTEGER
DECLARE INTEGER GetLastError IN WIN32API

nExeHwnd=CreateMutex(0,1,@pMutex)
IF GetLastError()=ERROR_ALREADY_EXISTS
=CloseHandle(nExeHwnd)
RETURN .F.
ELSE
RETURN .T.
ENDIF
ENDFUNC
***
Used in program:
cProgram=[program-name]
IF .not.FirstInstance(cProgram)
MessageBox(&quot;Program is already running&quot;+CHR(13)+&quot;canceled...&quot;,0+16,cProgram)
QUIT
ENDIF

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top