TheDcoder Posted April 5, 2015 Posted April 5, 2015 Hello , 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 ? 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
Danp2 Posted April 5, 2015 Posted April 5, 2015 You are overwriting the passed parameter with the global variable, which I suspect is the opposite of what you intended. Latest Webdriver UDF Release Webdriver Wiki FAQs
Moderators Melba23 Posted April 5, 2015 Moderators Posted April 5, 2015 TheDcoder,And referencing this thread, you are generating multiple instances, not multiple objects. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
TheDcoder Posted April 5, 2015 Author Posted April 5, 2015 @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
Moderators Melba23 Posted April 5, 2015 Moderators Posted April 5, 2015 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
TheDcoder Posted April 5, 2015 Author Posted April 5, 2015 (edited) @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 April 5, 2015 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
Moderators Melba23 Posted April 5, 2015 Moderators Posted April 5, 2015 (edited) 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. M23 Edited April 5, 2015 by Melba23 Fixed tags 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
TheDcoder Posted April 5, 2015 Author Posted April 5, 2015 And anyway, why are you using a function parameter which you immediately reassign within the function? It makes no sense at all. You will get to know it soon $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
Moderators Melba23 Posted April 5, 2015 Moderators Posted April 5, 2015 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Solution TheDcoder Posted April 5, 2015 Author Solution Posted April 5, 2015 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 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
Moderators Melba23 Posted April 5, 2015 Moderators Posted April 5, 2015 TheDecoder, I still wonder why my first code doesn't workI 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
TheDcoder Posted April 6, 2015 Author Posted April 6, 2015 @Melba23 Erm.... Why does this line: $sIdentifier = $sProgramIdentifier Not pass the string?? 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
iamtheky Posted April 6, 2015 Posted April 6, 2015 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 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Danp2 Posted April 6, 2015 Posted April 6, 2015 @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? Latest Webdriver UDF Release Webdriver Wiki FAQs
TheDcoder Posted April 6, 2015 Author Posted April 6, 2015 (edited) You overwrite the value of $sIdentifier ("Lick the lemon") with the value of $sProgramIdentifier (Undefined) 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 April 6, 2015 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now