Jump to content

Recommended Posts

Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted (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 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
Posted (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. :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:

  Reveal hidden contents

 

Posted
  On 4/5/2015 at 1:45 PM, Melba23 said:

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

 

  On 4/5/2015 at 12:36 PM, TheDcoder said:
$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
Posted

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:

  Reveal hidden contents

 

  • Solution
Posted

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

  • Moderators
Posted

TheDecoder,

 

  Quote

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:

  Reveal hidden contents

 

Posted

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
 
 

  Reveal hidden contents

Posted

  On 4/6/2015 at 12:10 PM, TheDcoder said:

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

$sIdentifier = $sProgramIdentifier

Not pass the string??

 

  On 4/5/2015 at 12:57 PM, Danp2 said:

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? 

Posted (edited)
  On 4/6/2015 at 12:28 PM, Danp2 said:
  • 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

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
×
×
  • Create New...