Jump to content



Photo

_SingletonPID() - Enforce only one instance of the program running. Use AutoIt's hidden window.


  • Please log in to reply
8 replies to this topic

#1 guinness

guinness

    guinness

  • MVPs
  • 10,252 posts

Posted 30 April 2012 - 10:03 AM

This is slightly different to how _Singleton (Misc.au3) works by Valik, because firstly this function uses the hidden window of an AutoIt executable and secondly it returns the PID of the main executable, not the handle to the object used for synchronization. It should also be noted that $iFlag parameter 2 doesn't exist in this function and if no program exists already _SingletonPID returns 0. Any suggestions are welcome. Thanks.

Function:
AutoIt         
; #FUNCTION# ==================================================================================================================== ; Name ..........: _SingletonPID ; Description ...: Enforce a design paradigm where only one instance of the script may be running. ; Syntax ........: _SingletonPID($sOccurenceName[, $iFlag = 0]) ; Parameters ....: $sOccurenceName      - String to identify the occurrence of the script. ;                  $iFlag               - [optional] Optional parameters. Default is 0. ;                   0 - Exit the script with the exit code -1 if another instance already exists. ;                   1 - Return the PID of the main executable and without exiting the script too. ; Return values .: Success - 0 No other process is running. ;                  Failure - The PID of the main executable. ; Author ........: guinness with initial ideas by Valik for _Singleton & KaFu for _EnforceSingleInstance. ; Example .......: Yes ; =============================================================================================================================== Func _SingletonPID($sOccurenceName, $iFlag = 0)     Local $hWnd = WinGetHandle($sOccurenceName)     If @error Then         AutoItWinSetTitle($sOccurenceName)         $hWnd = WinGetHandle($sOccurenceName)         ControlSetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1'), @AutoItPID)     Else         If BitAND($iFlag, 1) Then             Return Number(ControlGetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1')))         Else             Exit -1         EndIf     EndIf     Return 0 EndFunc   ;==>_SingletonPID

Example 1:
Local $iSingleton = _SingletonPID('myUniqueName', 1) If $iSingleton = 0 Then     MsgBox(4096, '', 'This is the first instance of the program running: ' & $iSingleton) Else     MsgBox(4096, '', 'There is another instance running. This PID is: ' & $iSingleton) EndIf

Example 2:
Local $iSingleton = _SingletonPID('myUniqueName', 1) If $iSingleton = 0 Then     MsgBox(4096, '', 'This is the first instance of the program running: ' & $iSingleton) Else     MsgBox(4096, '', 'There is another instance running. This PID is: ' & $iSingleton)     ; Wait for the process to close.     ProcessWaitClose($iSingleton, 5)     ; Reset _SingletonPID to current process since the previous one was closed.     $iSingleton = _SingletonPID('myUniqueName', 1)     MsgBox(4096, '', 'This is now the first instance of the program running: ' & $iSingleton) EndIf

Edited by guinness, 18 October 2012 - 01:32 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013






#2 qvex21

qvex21

    Seeker

  • Active Members
  • 28 posts

Posted 14 May 2012 - 07:48 PM

Great. I am just writing my first script in a productive environment and already needed a way to prevent multiple instances of the script, but not by preventing further intances from executing but by killing the older instances. So your 2 weeks old function just comes just at the right time for me.

dim $iScriptIsRunnig = _SingletonPID( "HWS-start-stop.exe", 1) If $iScriptIsRunnig Then ProcessClose( $iScriptIsRunnig)


Thanks a lot.
English is not my native language; please excuse typing errors.

#3 guinness

guinness

    guinness

  • MVPs
  • 10,252 posts

Posted 14 May 2012 - 07:53 PM

Great, didn't think anyone would use it. One point to make with your code is don't use Dim, try to use Global (if necessary) or Local scope.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#4 qvex21

qvex21

    Seeker

  • Active Members
  • 28 posts

Posted 14 May 2012 - 08:02 PM

Great, didn't think anyone would use it.


And i didn't think that my problem would be solved so quickly.

One point to make with your code is don't use Dim, try to use Global (if necessary) or Local scope.


Thanks for the hint.
English is not my native language; please excuse typing errors.

#5 qvex21

qvex21

    Seeker

  • Active Members
  • 28 posts

Posted 14 May 2012 - 09:27 PM

Local $iScriptIsRunnig = _SingletonPID( "HWS-start-stop.exe", 1) If $iScriptIsRunnig Then ProcessWaitClose( $iScriptIsRunnig) ; some code


MMh... When i execute my compiled script, it runs (ok). When i execute it again while the first instance is still running, it kills the older instance and runs (ok). Now only the new instance is running (looked up that in process explorer). But when i execute it again the third time, it doesn't kill the other instance any more.

Edited by qvex21, 14 May 2012 - 09:43 PM.

English is not my native language; please excuse typing errors.

#6 guinness

guinness

    guinness

  • MVPs
  • 10,252 posts

Posted 14 May 2012 - 09:43 PM

There is no bug, everything is working correctly, basically you need to reset _SingletonPID to the new process e.g.
Local $iSingleton = _SingletonPID('RandomName', 1) If $iSingleton = 0 Then     MsgBox(4096, '', 'This is the first instance of the program running: ' & $iSingleton) Else     MsgBox(4096, '', 'There is another instance running. This PID is: ' & $iSingleton)     ; Wait for the process to close.     ProcessWaitClose($iSingleton, 5)     ; Reset _SingletonPID to current process since the previous one was closed.     $iSingleton = _SingletonPID('RandomName', 1)     MsgBox(4096, '', 'This is now the first instance of the program running: ' & $iSingleton) EndIf

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#7 qvex21

qvex21

    Seeker

  • Active Members
  • 28 posts

Posted 15 May 2012 - 07:32 PM

There is no bug, everything is working correctly, basically you need to reset _SingletonPID to the new process e.g.

I thought that, when only the second instance is running and i start the third, the first line of my code should do the reset. Maybe i unterstand that later, i am just a beginner. Anyway, meanwhile i solved my problem this way:
$aProcList = ProcessList ( "HWS-start-stop.exe" ) For $i = 1 To $aProcList[0][0]     If $aProcList[$i][1] <> @AutoItPID Then ProcessClose($aProcList[$i][1]) Next

Edited by qvex21, 15 May 2012 - 07:40 PM.

English is not my native language; please excuse typing errors.

#8 guinness

guinness

    guinness

  • MVPs
  • 10,252 posts

Posted 15 May 2012 - 07:35 PM

I'm not following, what I provided you works.

Edit: Ah, I think you misunderstood the purpose of this function from what you posted.

Edited by guinness, 15 May 2012 - 07:41 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#9 guinness

guinness

    guinness

  • MVPs
  • 10,252 posts

Posted 18 October 2012 - 01:33 PM

Updated the function syntax and added a second example on how to close the PID and reset to the current process.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users