Is application running ?

In post-DOS Oss it’s possible running multiple applications, even multiple instance of same application simultaneously. Sometime running multiple instance of same application may cause unpredictable results. So requires impeding, For this purpose HMG offers SET MULTIPLE ON | OFF [WARNING] command which impedes (OFF) or allow (ON) attempts to run multiple instances of the application with optional warning for both situation.

Sometime may be requires decide to allowing at run time. In this case you need know either a running instance exist or not before allowing or impeding.

IsAppRunning() function is for help this situation.

#include <hmg.ch>

PROCEDURE Main()

   LOCAL cCurrApp := HB_ProgName()
   
   IF IsAppRunning( cCurrApp )
      MsgSTop( cCurrApp + CRLF + CRLF +;
               "Application is running already !", "Error")
   ELSE
      
      DEFINE WINDOW frmIsAppRunTest ;
         ROW 0 ;
         COL 0 ;
         WIDTH 650 ;
         HEIGHT 450 ;
         TITLE "Testing 'IsAppRunning()' function" ;
         ON INIT MsgInfo( cCurrApp + CRLF + CRLF + ;
                          "Application is running for first time !",;
                          "No problem") ;
         MAIN
      
         ON KEY ESCAPE ACTION ThisWindow.Release
      END WINDOW // frmIsAppRunTest

      frmIsAppRunTest.Center()

      frmIsAppRunTest.Activate()
   ENDIF
   
RETURN // IsAppRunTest.Main()      

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

PROCEDURE IsAppRunning(;
                        cAppName )

   LOCAL aProcess := EnumProcessesID(),;      // --> return array { nProcessID1, nProcessID2, ... }      
         nInstance := 0,;
         n1ProcID,;
         lRVal := .F.
         
   FOR EACH n1ProcID IN aProcess 
      IF cAppName == GetProcessFullName( n1ProcID ) 
         IF ++nInstance > 1
            lRVal := .T.
            EXIT 
         ENDIF   
      ENDIF                                      
   NEXT n1ProcID
   
RETURN lRVal // IsAppRunning()

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

File Management

File Management Function

DeleteFile Deletes an error-tolerant file
ExeName Full File Name of Executable ( Application )
File Tests for the existence of file(s)
FileMove Moves file(s) to another directory
FileDelete Deletes file(s) by name and attribute
FileSeek Searches for files by name and attribute
HB_FNameExists Detect File / Folder existence
HB_ProgName Returns name and directory of the current Harbour program
HB_FileExists Tests for the existence of a file
HB_FileMatch Makes a file comparison with mask
HB_FTempCreate Creates and opens a temporary file
HB_FTempCreateEx Creates and opens a temporary file with extension
RenameFile Fault tolerant renaming of a file
TempFile Creates a file for temporary use