Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Single instance app
Message
From
24/02/1999 03:30:32
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00190737
Message ID:
00190825
Views:
14
>I remember using the foxtools library to ensure that only one instance of my app was running, but I seem to have misplaced that code :)
>
>Has anyone else used foxtools to accomplish this and could you post / email me a sample of your code?
>
>TIA
This does not use foxtools but will make sure you have one instance :
LOCAL lhWnd,lhMutex,lcMutexName,llFound,llQuit

llFound=.f.

* Declare some API functions 
DECLARE Integer GetWindow in win32api Integer,Integer
DECLARE Integer ShowWindow in win32api Integer,Integer
DECLARE Integer GetDesktopWindow in win32api
DECLARE Integer SetForegroundWindow in win32api Integer
DECLARE Integer GetForegroundWindow in win32api
DECLARE Integer CreateMutex in win32api Integer,Integer,String @
DECLARE Integer CloseHandle in win32api Integer
DECLARE Integer GetLastError in win32api
DECLARE Integer SetProp in win32api Integer,String @,Integer
DECLARE Integer GetProp in win32api Integer,String @
DECLARE Integer RemoveProp in win32api Integer,String @
DECLARE Integer IsIconic in win32api Integer

* Define the Mutex
lcMutexName=const_APPLICATIONNAME+CHR(0)
lhMutex=CreateMutex(0,1,@lcMutexName)

if GetLastError()=183 
	* Mutex Already Exist - locate the original application 
	lhWnd=GetDesktopWindow() && handle to the desktop
	lhWnd=GetWindow(lhWnd,5) && get the first child of the desktop
	
	do while lhWnd>0 and !llFound && loop until we find the window or run out of windows
		if GetProp(lhWnd,@lcMutexName)==1
			if IsIconic(lhWnd)>0 
				ShowWindow(lhWnd,9)
			endif
			SetForegroundWindow(lhWnd)
			llFound=.T.
		endif
		lhWnd=GetWindow(lhWnd,2) && get handle to the next child of the desktop
	enddo
	CloseHandle(lhMutex)
	llQuit=.T.
else
	* this assumes our application window is currently active 
	lhWnd=GetForegroundWindow()
	
	* To help identify the application window we are adding
	* a property to it - which we can later search for
	SetProp(lhWnd,@lcMutexName,1)
	llQuit=.F.
endif

if llQuit
 * quit this instnace...
endif
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform