Jump to content

_Singleton Generates Multiple Objects!


Go to solution Solved by TheDcoder,

Recommended Posts

Hello :bye:, Please look at the code below:

; This is not the real script that I use for my project!!! This decoy script helps you understand better ;)

#include "Misc.au3"

Global $sProgramIdentifier

Func Test($sIdentifier)
    $sIdentifier = $sProgramIdentifier
    Switch _Singleton($sProgramIdentifier, 1)
        Case 0
            MsgBox(64, "Testing, Testing", "Its working!!")
        Case Else
            MsgBox(64, "Testing, Testing", "Its not working!!")
    EndSwitch
EndFunc

Test("Lick the lemon")

Save this to a file and run it, you will get a MsgBox telling "Its not working!!"

Again, Run the script a second-time without closing the first MsgBox and you will again get a MsgBox telling the same thing!! This was not supposed to happen :(

How can I get the MsgBox telling that it works :unsure:?

Thanks In Advance, TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDcoder,

And referencing this thread, you are generating multiple instances, not multiple objects. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Danp2 Overwritten or not, but the content does not change, does it? ;)

@Melba23 I don't quite understand the difference "instances" & "objects" here, please explain :)

TD

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDcoder,

Multiple instances are where the same app is running several times at the same time. You can see this with SciTE using the check.if.already.open option - if not set (which is the default SciTE4AutoIt3 setting) each file is opened in a new tab; if set each file is opened in a new instance of SciTE. Give it a try and see - but add the line to your User Properties file so you can delete it once you have tested. :)

Objects are something entirely different - and I suggest you do what >JohnOne suggested and do some Google research for yourself. You might want to start here. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23 I know what instances are, but in this case _singleton should return 0 because there are multiple instances (like in the example for _Singleton). I wanna know why _Singleton is retuning multiple mutex(es) instead of 0

TD :)

P.S Thanks for the wiki link

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDcoder,

_Singleton depends on you passing something which can create a mutex object which is then used to flag whether it is currently in use. In the script you posted, the string used by _Singleton to generate the mutex is empty - which from my limited knowledge means that the whole system falls over. If you assign a proper string to the mutex like this:

Global $sProgramIdentifier = "fred"
then the mutex object is successfully created and the second instance does indeed return "Its working!!". ;)

And anyway, why are you using a function parameter which you immediately reassign within the function? It makes no sense at all. :wacko:

M23

Edited by Melba23
Fixed tags

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

And anyway, why are you using a function parameter which you immediately reassign within the function? It makes no sense at all. :wacko:

You will get to know it soon :D

 

$sIdentifier = $sProgramIdentifier

Doesn't this line assign the variable a value?

TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDcoder,

Indeed it does - but the question is why you are immediately reassigning a function parameter to a Global variable. You use parameters to pass specific values to a function, or to assign default values if no parameter is passed. If you want to use a Global variable inside a function, then there is no point in doing what you did - just use the variable directly. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Solution

Thanks Melba!

This code works:

; NOTE: Removed line no.6 & modified line no.7 in the previous code :)

#include "Misc.au3"

Func Test($sIdentifier)
    Switch _Singleton($sIdentifier, 1)
        Case 0
            MsgBox(64, "Testing, Testing", "Its working!!")
        Case Else
            MsgBox(64, "Testing, Testing", "Its not working!!")
    EndSwitch
EndFunc

Test("Lick the lemon")

TD :D

P.S I still wonder why my first code doesn't work >_<

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • Moderators

TheDecoder,

 

I still wonder why my first code doesn't work

I thought I had already explained that. :)

You were resetting the function parameter which you use to create the mutex to the value of an undefined Global variable - mutexes need a string to work, so passing an empty string meant that the function would fail. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It does pass it and it is empty, lets try this a third time "Empty strings dont work!"  

Global $sProgramIdentifier
 
$sProgramId2 = ""
 
_test($sProgramIdentifier , $sProgramId2)
 
 
Func _test($sParam1 , $sParam2)
 
If $sProgramId2 == $sProgramIdentifier Then msgbox (0, '' , "those are the same thing and neither will work")
 
EndFunc
 
 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@Melba23 Erm.... Why does this line:

$sIdentifier = $sProgramIdentifier

Not pass the string??

 

You are overwriting the passed parameter with the global variable,  which I suspect is the opposite of what you intended. 

 

This is what I tried to point out in my initial post. Let's break this down into simple steps:

  • You declare a global variable named $sProgramIdentifier, but never assign it a value
  • You call the function Test with the parameter "Lick the lemon"
  • The string gets stored into the variable $sIdentifier
  • You overwrite the value of $sIdentifier ("Lick the lemon") with the value of $sProgramIdentifier (Undefined)

Can you explain the intended purpose of the global variable? Is it supposed to contain the most recent value passed to the Test function? 

Link to comment
Share on other sites

  • You overwrite the value of $sIdentifier ("Lick the lemon") with the value of $sProgramIdentifier (Undefined)

:idea: Sorry all, What I thought is:

$sIdentifier = $sProgramIdentifier

  ---------------->  = ----------------->

But I forgot that

<--------------- = <--------------------

Thanks, TD :)

Edit: Completed the post (pressed the submit button mistakenly)

Edit 2: Comma

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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
 Share

×
×
  • Create New...