Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Finding If a program is running
Message
 
À
05/09/2005 01:20:59
Sunil Kejariwal
The Software Workshop
Mumbai, Inde
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 6
Divers
Thread ID:
01046715
Message ID:
01046728
Vues:
16
Sunil,

>
> I want to check in the begining of my program weather a particular exe is already running or not ?
> If it is not running then I want to run it programatically .
>

I do have an EXE where I only want to allow one instance. This is the function I use to check wether it already runs. If yes, then I shutdown the APP, otherwise I continue. As You can see, the code is already veeeeeery old, but still does the job. It has german Comments. If You need help, just ask
*=========================================================
*
*         FUNCTION: TaskRuns
*
*=========================================================
FUNCTION TaskRuns
*  Author............:  Dipl.Kfm. Frank Dietrich
*  Copyright.........:  (c) Frank Dietrich Datentechnik, 1995
*                       Leo-Baeck-Str. 22
*                       D-14165 Berlin
*                       CIS: 100322,333
*                       frank.dietrich@dd-tech.de
*  Created...........:  31.Oktober 1995, 15:31 Uhr
*  Changed...........:    02.11.96
*                        Es kann jetzt ein bestimmter Window-
*                        handle mitübergeben werden, falls
*                        der Betrachtete Task bereits läuft
*                        und oben liegt
*                        11.06.1997
*                        Änderung für VFP mit DECLAREs anstatt
*                        über FOXTOOLS
*  Description.......:  Funktion, die überprüft, ob ein bestimmter 
*                        Task (Fenstertitel) läuft oder nicht
*  Dependencies......:  
*  Calling Samples...:  ? TaskRuns(<ExpC>)
*  Parameters........:  tc_TaskName, tn_MyHandle
*  Returns...........:  .T., wenn Task aktiv, .F., wenn nicht
lparameters    tc_TaskName, tn_MyHandle
local ll_Found, ll_RetVal, lc_Buffer

ll_RetVal = .F.

if empty(tc_TaskName)
    wait wind "Ooops... Kein Taskname übergeben"
    return ll_RetVal
endif
    
if empty(tn_MyHandle)
    *// Registrieren der Windows API-Funktion: GetActiveWindow
    declare INTEGER GetActiveWindow in WIN32API

    *// und erfragen des Wertes des aktiven Fensters
    iOldhWnd    = GetActiveWindow()
else
    iOldhWnd    = tn_MyHandle
endif        

*// Registrieren der Windows API-Funktion: GetWindow
declare INTEGER GetWindow in WIN32API INTEGER, INTEGER

*//Registrieren der Windows API-Funktion: GetWindowText
declare INTEGER GetWindowText in WIN32API INTEGER, STRING @, INTEGER

*-- These would be needed if I wanted to bring a running App to front
*--    *Registrieren der Windows API-Funktion: ShowWindow
*--    ShowWin = RegFn("ShowWindow", "II", "I", "USER")

*--    *Registrieren der Windows API-Funktion: BringWindowToTop
*--    BringWinTop = RegFn("BringWindowToTop", "I", "I", "USER")

*// GetWindow Konstanten
#define GW_HWNDFIRST  0        && erstes Fenster im Task
#define GW_HWNDNEXT   2        && nächstes Fenster im Taskstack

*// ShowWindow Konstanten
*SW_SHOWNORMAL         = 1
*SW_SHOWMAXIMIZED     = 3

*// Belegt die Variable lc_Buffer mit 255 Zeichen
lc_Buffer             = REPLICATE(CHR(0), 255)

ihWnd  = GetWindow(iOldhWnd, GW_HWNDFIRST)

*// wait wind str(ihWnd)

DO WHILE ihWnd <> 0 && Taskschleife durchlaufen
     *--
      *-- erfragt den Fenstertext des entsprechenden Tasks
      *--
     iResult = GetWindowText(ihWnd, @lc_Buffer, LEN(lc_Buffer))

     IF iResult > 0     && Ja, Eintrag vorhanden
        *wait wind left(lc_Buffer, iResult) nowa
         IF LEFT(lc_Buffer, iResult) = tc_TaskName
             *--
             *-- Wenn erfragte Fensterüberschrift gleich
             *-- Überschrift des gesuchten Task, dann return ll_RetVal = .T.
            *--
            ll_RetVal = .T.             
             exit
         ELSE
             *--
             *-- Buffer wieder leeren und nächsten Task abfragen
             *--
             lc_Buffer = REPLICATE(CHR(0), 255)
             ll_RetVal = .F.
         ENDIF
     ENDIF

     iOldhWnd = ihWnd
     *// erfragt den Wert des nächsten Fensters aus der Tastliste,
     *// basierend auf den Wert des vorherigen Fensters
    ihWnd = GetWindow(iOldhWnd, GW_HWNDNEXT)
         
ENDDO

*-- this would be bringing the other to front
*// iResult = CallFn(BringWinTop, iMerkhWnd)

flush

return ll_RetVal
tc_Task is Your Application's title that is shown in the Titlebar. (In VFP that would be "Microsoft Visual FoxPro"

And this is how I call it:
    lc_TaskTitle = "My Application"
    ll_Retval    = .T.

    if TaskRuns(lc_TaskTitle)
        *-- DD changed on 17.04.00
        *-- MsgBox, die sich selbst wieder entfernt
        do FORM SCREEN\TimedMSGBox.SCX with ;
             "Das Programm "+ lc_TaskTitle +;
             " läuft bereits in einem anderen Task auf dieser Station."+;
             chr(13)+chr(10) +"Bestätigen Sie diese Meldung mit ENTER "+;
             "oder klicken Sie mit der Maus auf OK und wechseln dann "+;
             "mit ALT+TAB zu der gewünschten Anwendung.", ;
             48, "Information", 6000

        ll_RetVal = .F. 
    endif     

    if ! ll_RetVal
        *-- clean up and quit Your app
    endif
HTH
Regards from Berlin

Frank

Dietrich Datentechnik (Berlin)
Softwarekombinat Teltow (Teltow)

Frank.Dietrich@dd-tech.de
DFPUG # 327
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform