Is there a simple method by which a program can ensure that there at any time is only one instance of another program running? Thanks in advance. 
Best regards,
Erik

Best regards,
Erik
$MUTEX_NAME = "whatever" FUNCTION WinMain(...) SzMutex = $MUTEX_NAME hMutex = OpenMutex (....szMutex no_ownership_needed) IF ISTRUE Mutex THEN 'program already running , return error code and exit CloseHandle hMutex FUNCTION = 1 EXIT FUNCTION ELSE hMutex = CreateMutex (..) ' next program instance will be able to open the mutex forcing it to exit END IF ' here there is a debate... you can CloseHandle (hMutex), or you can let ' windows do it for you when your process completely ends. ' END FUNCTION
Case %WM_INITDIALOG '<- for processing that needs to occur when the program loads 'Check to see if program already running Local hmutex&, smutex$ 'just used for testing program existence smutex$ = " Checkbook" 'Program name hmutex& = CreateMutex(ByVal %Null, 0, ByVal StrPtr(smutex$))'check if running If hmutex& <> %Null Then 'Program probably already running If GetLastError() = %error_already_exists Then 'Constant defined in WinAPI MsgBox SmuTex$ & " is already running", %Top, Using$("Error code #",Err) Dialog Post CbHndl, %Dialog_End, 0, 0 'Tell program not to run 'Can't end it in %WM_InitDialog 'Tell it to end in %WM_Command Exit Function End If End If 'Continue Any initializing code here
Local PrgName As Asciiz * 255 Local hInstanceMutex& PrgName = "My_Program_Name" ' Important hInstanceMutex = CreateMutex(ByVal %Null, 0, PrgName) If hInstanceMutex = 0 Then Exit Function ' Error in Mutex If GetLastError = %ERROR_ALREADY_EXISTS Then MsgBox "This old Program is already running" Exit Function End If
FUNCTION PBMAIN () AS LONG OPEN "wait.dat" FOR BINARY AS #9999 IF ERR THEN ? "Sorry, in use":EXIT FUNCTION ? "I'm running" END FUNCTION
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment