Results 1 to 7 of 7

Thread: How to prevent running same script more than once?

  1. #1
    Junior Member
    Join Date
    Dec 2023
    Posts
    21
    Rep Power
    4

    How to prevent running same script more than once?

    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.

  2. #2
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    57
    Rep Power
    15
    Eros describes how to do this using a MUTEX.

    Ref: https://www.thinbasic.com/community/...e-copy-running

    Joe

  3. #3
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    57
    Rep Power
    15
    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.

  4. #4
    Junior Member
    Join Date
    Dec 2023
    Posts
    21
    Rep Power
    4
    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.

  5. #5
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    57
    Rep Power
    15
    Quote Originally Posted by TheInsider View Post
    PS. For future readers, you might want to edit that typo (dworddim) in the first line.
    Code has been fixed, thankyou for catching that.

    For whatever reason, it pasted without a LF/CR in that line when copying to the forum.

    Joe
    Last edited by Joe Caverly; 15-06-2025 at 17:43.

  6. #6
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    57
    Rep Power
    15
    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 free
    
    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)
    
    Now we know!

    Joe

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    58
    Posts
    8,849
    Rep Power
    10
    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

Similar Threads

  1. Running CGI
    By alberto in forum CGI and FastCGI
    Replies: 0
    Last Post: 04-01-2019, 00:20
  2. Bundle .exe needs UAC? Any way to prevent this?
    By EmbeddedMan in forum thinBundle
    Replies: 18
    Last Post: 16-12-2016, 18:52
  3. How to prevent Dialog from closing before confirmation?
    By Michael Hartlef in forum UI (User Interface)
    Replies: 2
    Last Post: 03-10-2008, 15:09
  4. only one copy running
    By sandyrepope in forum thinBasic General
    Replies: 4
    Last Post: 13-07-2007, 17:33

Members who have read this thread: 8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •