Eros describes how to do this using a MUTEX.
Ref: https://www.thinbasic.com/community/...e-copy-running
Joe
I need my script to run as 'single instance' (Meaning it will end if the same script is already running), but my search for a simple way is coming up empty.
Autohotkey has the #SingleInstance directive, which makes it a simple one-line deal.
I know I can do things such as creating/checking for a dummy file, etc. Just wondered if there is an established method.
Eros describes how to do this using a MUTEX.
Ref: https://www.thinbasic.com/community/...e-copy-running
Joe
Example source code;
dim Mutex_Handle as dword dim Mutex_Name as string = "UniqueMutexName" Mutex_Handle = APP_MutexCreate(Mutex_Name) if Mutex_Handle = 0 then msgbox 0, "Another instance of the script is already running.", %MB_ICONERROR stop else msgbox 0, "Script is running. Mutex created successfully.", %MB_ICONINFORMATION end if '---At the end close the mutex handle to release it for new script execution APP_MutexClose(Mutex_Handle)
Joe
Last edited by Joe Caverly; 15-06-2025 at 17:36.
Thank you Joe. That is exactly what I was looking for.
I did search the forums, sample programs, Google, etc. I recall seeing 'Mutex' in the search, but since I didn't understand what I was looking at, I passed it by.
It's quite simple to use now that I see your example code.
PS. For future readers, you might want to edit that typo (dworddim) in the first line.![]()
Last edited by TheInsider; 15-06-2025 at 16:29.
There was no need to scoure the internet,
as thinbasic includes a sample called MUTEX.tbasic
Volume in drive E is New Volume Serial number is 2c1e:6e61 Directory of E:\thinBasic\SampleScripts\General\MUTEX.tbasic 2019-02-06 7:12 1,032 MUTEX.tbasic 1,032 bytes in 1 file and 0 dirs 4,096 bytes allocated 1,335,186,173,952 bytes freeNow we know!dim hMutex as dword dim Mutex_Name as string = "AVeryUniqueStringToNameMutex" '---Better if MutexName has script name inside so it will be specific to this script Mutex_Name += app_scriptname hMutex = APP_MutexCreate(Mutex_Name) if hMutex = 0 then msgbox 0, _ "I was not able to create a Mutex named: " & Mutex_Name & $crlf & _ "This means that another instance of the same script is already running" & $crlf & _ "or you have not enough user authorization to create a Mutex." & $crlf & _ "In any case script execution is aborted. Sorry", %MB_ICONERROR stop else msgbox 0, _ "I was able to create a Mutex named: " & Mutex_Name & $crlf & _ "Mutex handle is " & hMutex & "" & $crlf & _ "Script will continue. Perfect", %MB_ICONINFORMATION end if '... '------ '---Here code for script ... '--- '... '---At the end close the mutex handle to release it for new script execution APP_MutexClose(hMutex)
Joe
Hi Joe, thanks a lot for supporting TheInsider !
All perfect.
Appreciated.
Last edited by ErosOlmi; 16-06-2025 at 15:09.
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000