Sign in to follow this  
Followers 0
JAFN

_singleton question?

15 posts in this topic

I have a script that may have several instances running.

I would like to have them dock top to bottom and also for only the first instance to record its location.

Is there a way to access the order of previous instances and to get their locations?


[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have a script that may have several instances running.

I would like to have them dock top to bottom and also for only the first instance to record its location.

Is there a way to access the order of previous instances and to get their locations?

You could do something like this

#include <misc.au3>

Global $instance = True, $inst = 0
While $instance = True
    $inst += 1
    _Singleton("MySpecialAppxyz" & $inst, 1)
    $instance = @error = 183
WEnd

$ht = 200
$wd = 200
$wintop = 0

If $inst > 1 Then
    $pos = WinGetPos("SameTitle");gets pos of first inst
    ;$pos = wingetpos(""DifferentTitle" & $inst - 1)
    $wintop = $pos[1] + $pos[3]
EndIf

$Gui = GUICreate("SameTitle", $ht, $wd, 0, $wintop)
;$Gui = GUICreate("DifferentTitle" & $inst,$ht,$wd,0,$ht*($inst - 1))
GUISetState()
While GUIGetMsg() <> -3
WEnd

But if any of the windows is closed it might mess things up, so you could check every so often. I think I would register a unique message and set things so that if an instance closes it closes all instances after it.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

You could do something like this

#include <misc.au3>

Global $instance = True, $inst = 0
While $instance = True
    $inst += 1
    _Singleton("MySpecialAppxyz" & $inst, 1)
    $instance = @error = 183
WEnd

$ht = 200
$wd = 200
$wintop = 0

If $inst > 1 Then
    $pos = WinGetPos("SameTitle");gets pos of first inst
    ;$pos = wingetpos(""DifferentTitle" & $inst - 1)
    $wintop = $pos[1] + $pos[3]
EndIf

$Gui = GUICreate("SameTitle", $ht, $wd, 0, $wintop)
;$Gui = GUICreate("DifferentTitle" & $inst,$ht,$wd,0,$ht*($inst - 1))

Wow. I was expecting that if it could be done it would be long and arcane.

I was able to adapt it to my program with minimal trouble and it works perfectly.

However I do not understand this important section

While $instance = True
    $inst += 1
    _Singleton("MySpecialAppxyz" & $inst, 1)
    $instance = @error = 183
WEnd

Is '"MySpecialAppxyz"' simply a title unlikely to be found?

And how even if so, I don't get what this section does.


[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

Yes, singleton registers a mutex which is (my undertsnading) a mutually exclusive string. It ties a mutex handle to the string. So if you register a string which hopefully only you would have in your script then you can see if windows has a record of this string. The process which first creates the mutex destroys it when it closes. If windows has a record of the string then there must be another of your scripts running.

In this case we don't just want to know if an instance is running but we want to know how many. So I added a number to the end of the string. (So make sure the original string doesn't end wich a digit.) We try _Singleton to see if the string has been registered and if it has, because _Singleton with the flag set to 1 gives an error of 183, then we try the next number. As soon as _Singleton doesn't set error to 183 we know that we are that number. The error = 183 means that the mutex already exists.

I hope that makes sense.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Yes, singleton registers a mutex which is (my undertsnading) a mutually exclusive string. It ties a mutex handle to the string. So if you register a string which hopefully only you would have in your script then you can see if windows has a record of this string. The process which first creates the mutex destroys it when it closes. If windows has a record of the string then there must be another of your scripts running.

In this case we don't just want to know if an instance is running but we want to know how many. So I added a number to the end of the string. (So make sure the original string doesn't end wich a digit.) We try _Singleton to see if the string has been registered and if it has, because _Singleton with the flag set to 1 gives an error of 183, then we try the next number. As soon as _Singleton doesn't set error to 183 we know that we are that number. The error = 183 means that the mutex already exists.

I hope that makes sense.

If I understand you right all that part of the code does is determine by '$inst > 1' that there are at least one other instance running.

Correct?


[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

If I understand you right all that part of the code does is determine by '$inst > 1' that there are at least one other instance running.

Correct?

Not to see if another is running but to count how many. Each new instance registers a new string.

When a script runs it tries to register a string MySpecialAppxyz1

If that is successful then this is the first script, and now we have MySpecialAppxyz1 registered.

Now another script is started. It also tries to register MySpecialAppxyz1 but that produces an error, so there must be at least one instance running.

Then it tries to register MySpecialAppxyz2 and that is successful and now we also have MySpecialAppxyz2 registered.

Now another script is started. It also tries to register MySpecialAppxyz1 but that produces an error, so there must be at least one instance running. Then it tries to register MySpecialAppxyz2 and that is successful and now we also have MySpecialAppxyz1 but that produces an error, so there must be at least two instances running which have already registered MySpecialAppxyz1 and MySpecialAppxyz2, so it tries MySpecialAppxyz3... and so on.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Not to see if another is running but to count how many. Each new instance registers a new string.

When a script runs it tries to register a string MySpecialAppxyz1

If that is successful then this is the first script, and now we have MySpecialAppxyz1 registered.

Now another script is started. It also tries to register MySpecialAppxyz1 but that produces an error, so there must be at least one instance running.

Then it tries to register MySpecialAppxyz2 and that is successful and now we also have MySpecialAppxyz2 registered.

Now another script is started. It also tries to register MySpecialAppxyz1 but that produces an error, so there must be at least one instance running. Then it tries to register MySpecialAppxyz2 and that is successful and now we also have MySpecialAppxyz1 but that produces an error, so there must be at least two instances running which have already registered MySpecialAppxyz1 and MySpecialAppxyz2, so it tries MySpecialAppxyz3... and so on.

How does it get registered? Does _Singleton do more than I thought it did?


[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

How does it get registered? Does _Singleton do more than I thought it did?

Evidently.

Have a look at the _Singleton function in misc.au3.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Evidently.

Have a look at the _Singleton function in misc.au3.

Oh that hurt my head.

But based on what I have I am going to try to write an aligning routine to run when each new instance opens.

Be back much later, most likely.

Thank you.


[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

Another way.

#Include <WinAPIEx.au3>

$hSemaphore = _WinAPI_CreateSemaphore('MySemaphore', 0, 65535)
$Count = _WinAPI_ReleaseSemaphore($hSemaphore) + 1

_MyGUI('#' & $Count)

_WinAPI_CloseHandle($hSemaphore)

Func _MyGUI($sTitle)

    Local $Msg

    GUICreate($sTitle)
    GUISetState()
    While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case -3
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>_MyGUI

Share this post


Link to post
Share on other sites

Another way.

#Include <WinAPIEx.au3>

$hSemaphore = _WinAPI_CreateSemaphore('MySemaphore', 0, 65535)
$Count = _WinAPI_ReleaseSemaphore($hSemaphore) + 1

_MyGUI('#' & $Count)

_WinAPI_CloseHandle($hSemaphore)

Func _MyGUI($sTitle)

    Local $Msg

    GUICreate($sTitle)
    GUISetState()
    While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case -3
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>_MyGUI

I like that method, but I don't understand what happens when a process closes. Can the semaphore count be decremented?

Th edisadvantage I suppose is that you can't tell if the third instance is running but the second is not..


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Okay.

I have 3 windows all starting with (-).

Why does the comment line never get reached?

OrderWindow()

Func OrderWindow()

    $Marker = "(-)"
    $First = True
    $List = ProcessList()
    $Items = $List[0][0]
;   MsgBox(0,"",$Items)
    If $Items < 1 Then Return
    For $x = $Items To 1 Step -1
        If $Marker <> $List[$x][0] Then ContinueLoop
        ; code never gets here
    Next
EndFunc

[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

OK, then you can use AutoIt window.

Opt('WinTitleMatchMode', 1)
Opt('WinWaitDelay', 0)

$Count = 1
While WinExists('MyUnique' & $Count)
    $Count += 1
WEnd

_MyGUI($Count)

$Title = AutoItWinGetTitle()
$Count = Number(StringMid($Title, 9, StringInStr($Title, '@') - 8))
While 1
    $Count += 1
    $hWnd = WinGetHandle('MyUnique' & $Count)
    If @error Then
        ExitLoop
    EndIf
    $Title = WinGetTitle($hWnd)
    WinSetTitle(HWnd(StringTrimLeft($Title, StringinStr($Title, '@'))), '', '#' & ($Count - 1))
    If @error Then
        ExitLoop
    EndIf
    WinSetTitle($hWnd, '', StringReplace($Title, $Count & '@', ($Count - 1) & '@', 1))
    If @error Then
        ExitLoop
    EndIf
WEnd

Func _MyGUI($iIndex)
    $hWnd = GUICreate('#' & $iIndex, 200, 200)
    AutoItWinSetTitle('MyUnique' & $Count & '@0x' & Hex($hWnd))
    GUISetState()
    Do
    Until GUIGetMsg() = -3
    GUIDelete()
EndFunc   ;==>_MyGUI
Edited by Yashied

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

My mind is fried now. Can't even figure out who is talking to who now.

I'll tackle it fresh in the morning.

Thank you.

Edited by JAFN

[size="2"]The second mouse gets the cheese[/size]

Share this post


Link to post
Share on other sites

Yashied, the second Example was very interesting indeed :unsure: Nice work.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_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()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_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()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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