View Full Version : only one copy running
sandyrepope
13-07-2007, 03:29
Is it possible for a script to tell if a copy is already running and not run again? What I mean is can you make sure that only one copy of the program is running at a time?
Thanks
Sandy
ErosOlmi
13-07-2007, 07:10
No, is not possible to detect. Every thinBasic execution is a separated process.
The same is standard windows applications. It is programmer responsability to check it and make relevant changes in the code to prevent it.
How you can do? There is a common technique that consists into creating an unique operating system identifier called MUTEX. Mainly when you start you script you first try to create a MUTEX with a special name. If creation was OK it means you can go on, execute you script and delete the MUTEX at the end. If creation fails it means some other process are using the same MUTEX name, so it means your script is already running and you can prevent second execution of it.
I will prepare some new APP_ functions to be able to create, check and destroy MUTEX very easily.
Ciao
Eros
ErosOlmi
13-07-2007, 07:53
Sandy,
please find a preview version of thinCore.dll attached to this post. I've implemented 2 new functions to be able to create and destroy MUTEX.
Please download and substitute the one you already have into \thinBasic\ directory. These functions will be present in next release.
Here a testing script for you to see how to use new functions. Try to execute the script and leave the message box open. Than start again the script and see.
Hope all is OK.
Ciao
Eros
dim Mutex_Handle 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
Mutex_Handle = APP_MutexCreate(Mutex_Name)
if Mutex_Handle = 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 " & Mutex_Handle & "" & $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(Mutex_Handle)
sandyrepope
13-07-2007, 17:26
I just tried the new feature and it works great. I've never heard of MUTEX but I'm glad to have it in thinBasic. This language just keeps getting better all the time.
Thanks
Sandy
ErosOlmi
13-07-2007, 17:33
Perfect. New features will be present in next release.
I'm not sure about final name of APP_MutexClose. Maybe better to call it APP_MutexRelease. Maybe I will keep both.
Mutex reference in MSDN: http://msdn2.microsoft.com/en-us/library/ms684266.aspx
There are so many thing in Windows that's almost impossible to know even a part.
Ciao
Eros