I use this in my scripts and compiled executables and it works about 99.9% of the time.
DirPath = DirScript ()
INIFile = "%DirPath%Control.ini"
Running = IniReadPvt ("General", "Running"," ", INIFile) ; Running 0 = NOT Running 1 = Running
If Running == "1" then RunState = "RUNNING"
If Running != "1" then RunState = "NOT RUNNING"
If Running == "1" then Message ("ERROR!", "Program already running...")
If Running == "1" then Exit
Running = IniWritePvt ("General", "Running","1", INIFile)
When (IF) it fails the quick fix is to delete the "Control.ini" file in the program/script directory.
EDIT -> At the end of the script/program in the "Exit" section I place this line:
Running = IniWritePvt ("General", "Running","0", INIFile)