Sign in to follow this  
Followers 0
pileot

Run One Instance

3 posts in this topic

In windows XP i had this script that would only allow one instance of a script to run. Its a bit messy, im no expert, but it was simple, small, and worked.

In windows 7 (what im using now) it does not work, and im interested if anyone has suggestions how to make it better. The idea is: If another existance of the same script is running, close that existance. Then rename the current script to the name i was checking for.

Opt("WinTitleMatchMode", 3)     ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

$Title = "NewNAME"
If WinExists($Title)=1 Then processclose($Title)
AutoItWinSetTitle($Title)

Share this post


Link to post
Share on other sites



Look at _Singleton in help file.

Enforce a design paradigm where only one instance of the script may be running.

#Include <Misc.au3>

_Singleton($sOccurenceName [, $iFlag = 0])

Parameters

$sOccurenceName String to identify the occurrence of the script. This string may not contain the \ character unless you are placing the object in a namespace (See Remarks).

$iFlag [optional] Behavior options.

0 - Exit the script with the exit code -1 if another instance already exists.

1 - Return from the function without exiting the script.

2 - Allow the object to be accessed by anybody in the system. This is useful if specifying a "Global\" object in a multi-user environment.

Return Value

Success: The handle to the object used for synchronization (a mutex).

Failure: 0

Remarks

You can place the object in a namespace by prefixing your object name with either "Global\" or "Local\". "Global\" objects combined with the flag 2 are useful in multi-user environments.


They call me MrRegExpMan

Share this post


Link to post
Share on other sites

Look at _Singleton in help file.

Enforce a design paradigm where only one instance of the script may be running.

Singleton usage usually involves closing the current instance of a script if a previous one exists. The goal of the original code was to close the OLD instance when the NEW one exists.

This was made by (I believe)Trancexx

#include <Misc.au3>
If _Singleton("apples are good", 1) = 0 Then
    ; KILL EXISTING PREVIOUS INSTANCE OF MyScript
    $aProc = ProcessList(StringRegExpReplace(@AutoItExe, ".*\\", ""))
    For $i = 0 To UBound($aProc) - 1
        If $aProc[$i][1] <> @AutoItPID Then ProcessClose($aProc[$i][1])
    Next
EndIf
While 1
    sleep(100)
WEnd
1 person likes this

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0