Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A New Twist On Is App Running
Message
De
31/05/2007 04:12:39
 
 
À
31/05/2007 01:27:21
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00409058
Message ID:
01229508
Vues:
18
Hi Jos,

On the _VFP.ServerName you need to fix the code like this for it to work:
nEH = CreateEvent(0,0,1, CHRTRAN(_VFP.ServerName + '.EVENT',"\","_"))
See http://msdn2.microsoft.com/en-us/library/ms682396.aspx for info about the \ character in event names.

On the PROGRAM(0) not working I have one or two ideas but without code to look at they are just guesses. But I would guess that either PROGRAM(0) is returning something different for each instance, or it is returning Local\something or you are some how releasing the handle before the the application quits.

I would check that CloseHandle(nEH) and CLEAR ALL are definitely not being called. And see what PROGRAM(0) is returning.

You can also hard code an event name like:
nEH = CreateEvent(0,0,1,'JosPolsIsTestingThis.Event')
Here is runnable code for testing:
***********************************************************************
*TestEvent.prg  if you name it something else change the DO TestEvent
*line to what you named this.
***** No Dual Instances Code ******************************************

LOCAL lnEH

IF TYPE("nDepth") = "U"
  nDepth = 0
  CLEAR
  ? "First Call creates an event named: "+PROGRAM(0) + '.EVENT'  
ELSE
  ? "Starting the Recursive Call: "+PROGRAM(0) + '.EVENT'    
ENDIF
IF nDepth>1
  ? "Error I was allowed to Execute"  
  RETURN
ENDIF

lnEH = CreateEvent(0,1,1, PROGRAM(0) + '.EVENT')

IF GetLastError() = 183 OR lnEH = 0
   ? "Quiting because we are Already Running",lnEH 
   *  There's an instance running already (183) or the Event can't be defined
   *  So don't do it;  do release the handle, since it's harmless and should
   *  be done rather than relying on Windows to clean up after itself
   =CloseHandle(lnEH)
   RETURN
ENDIF

nDepth = nDepth + 1 
? "Doing Recursive Call",lnEH 
DO TestEvent

*  While shutting down, issue:
=CloseHandle(lnEH)
RETURN
John

>Hi Stacy and All,
>
>I found this same problem under Vista but I cannot make Ed's code work for me. If I pass program(0) as the parameter to CreateEvent() then I can still open multiple copies of my program. Each gives a non-zero handle and GetLastError() returns 0. If I pass _VFP.ServerName as the parameter to CreateEvent() then the very first instance says that a copy is already running i.e. the one that is now doing the test! Any idea how to get around this?
>
>
>>Ed, thanks for the code example here, still helping folks with your info...
>>
>>We found out recently that the older versions based on a windows handle no longer operate properly under Vista if the application is minimized during use. The handle does not appear to be released when the app is closed.
>>
>>Your sample listed here appears to work perfectly!
>>
>>
>>>>I have an application that runs in the background. There is no user intervention via the application. One issue I have is people running the application more than one cause they cannot see it is running. I need to be able to detect the running app without using the Title bar as part of the solution. So I can just ignore someone trying to run it again.
>>>>
>>>>
>>>>Any Ideas? George :)
>>>>
>>>
>>>I'm not George, but I like using the native Event object for the OS via a couple of API calls:
>>>
>>>
DECLARE INTEGER CreateEvent IN WIN32API ;
>>>   INTEGER lpEventAttributes, ;
>>>   SHORT bManualReset, ;
>>>   SHORT bInitialState, ;
>>>   STRING @ lpName
>>>DECLARE INTEGER GetLastError IN Win32API
>>>DECLARE CloseHandle IN Win32API INTEGER hObject
>>>nEh = CreateEvent(0,0,1, PROGRAM(0) + '.EVENT')
>>>IF GetLastError() = 183 OR nEh = 0
>>>   *  There's an instance running already (183) or the Event can't be defined
>>>   *  So don't do it;  do release the handle, since it's harmless and should
>>>   *  be done rather than relying on Windows to clean up after itself
>>>   =CloseHandle(nEh)
>>>   QUIT
>>>ENDIF
>>>
>>>*  While shutting down, issue:
>>>=CloseHandle(nEh)
>>>
>>>This has the advantage that it derives it's Event name from the executable, and if you do forget to issue a CloseHandle(), it still releases the Event object without bleeding, and the Event object completely disappears automagically when no remaining Event refs exist. CLOSE ALL will release the Event. You can use the Event for synchronizing several processes or thread using WaitForSingleObject() to wait on someone to 'raise' the event if you need some sort of event monitoring. Works under all OS versions I tested - not checked under Win95, but does work on Win98 and ME, as well as NT and 2K.
>>>
>>>You can use any name you want if linking to the app's initial program doesn't work for you - I chose that because it's easy to get right, and presumably you want to block other instances of the app you're starting.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform