1282 Simultaneous script execution corrupts variables [6404]
Moderator: Gurus
1282 Simultaneous script execution corrupts variables [6404]
If I launch a script and then before completion launch it again, the variable writes within the second instance overwrite the first instance's variables.
Tracked at http://www.ventismedia.com/mantis/view.php?id=6404 .
Tracked at http://www.ventismedia.com/mantis/view.php?id=6404 .
Chris
Re: 1282 Simultaneous script execution corrupts variables
At http://www.ventismedia.com/mantis/view.php?id=6404 Jiri wrote:
and I repliedIt's actually designed this way to prevent problems with several instances of scripts running - there's always only one instance of a script.
Am I the only one finding I can launch two instances of a script and both show on the status bar?> It's actually designed this way to prevent problems
> with several instances of scripts running
It doesn't prevent problems AFAICS. It causes them.
> there's always only one instance of a script.
Not true. I can launch two instances and both show on the status bar.
Chris
Re: 1282 Simultaneous script execution corrupts variables
I know that I can, and I design my scripts to account for this.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
> I know that I can
Jiri, if you are reading this, please note: I think your statement is in error. And so I hope you will remove your RESOLVED status from this serious issue.
> and I design my scripts to account for this.
I have a scripting using many Dim variables that get corrupted by subsequent simultaneous instance. What's the best way allow two to run without this corruption? Failing which, to prevent two from running? Thanks.
Jiri, if you are reading this, please note: I think your statement is in error. And so I hope you will remove your RESOLVED status from this serious issue.
> and I design my scripts to account for this.
I have a scripting using many Dim variables that get corrupted by subsequent simultaneous instance. What's the best way allow two to run without this corruption? Failing which, to prevent two from running? Thanks.
Chris
Re: 1282 Simultaneous script execution corrupts variables
By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance as you have discovered because the global variables are the same references, therefore Jiri is correct in what he has said.
I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects". Sometimes I define a lot of global variables, for instance one for each of the settings I've stored in "MediaMonkey.ini", but I read them in from the ini file (or update them from the dictionary object) at the beginning of each Sub to make sure they are correctly populated.
The first thing a number of my scripts (such as "Genre Finder") do is check to see if SDB.Objects is already populated with a relevant dictionary and exits if it does.
I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects". Sometimes I define a lot of global variables, for instance one for each of the settings I've stored in "MediaMonkey.ini", but I read them in from the ini file (or update them from the dictionary object) at the beginning of each Sub to make sure they are correctly populated.
The first thing a number of my scripts (such as "Genre Finder") do is check to see if SDB.Objects is already populated with a relevant dictionary and exits if it does.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
> By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance
Ah. You actually said that in response to my
"Am I the only one finding I can launch two instances of a script ..."
> I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects".
Wow. That would really complicate and obfuscate my code. Likewise moving globals into locals, passing by reference.
Ah. You actually said that in response to my
"Am I the only one finding I can launch two instances of a script ..."
> I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects".
Wow. That would really complicate and obfuscate my code. Likewise moving globals into locals, passing by reference.
Chris
Re: 1282 Simultaneous script execution corrupts variables
I guess it depends on the interpretation of "instance", I wasn't thinking technically when I first responded. I always use SDB.Objects, seems to work well for me.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
> I always use SDB.Objects, seems to work well for me.
Thanks for the suggestion.
How would code using Dictionary objects this for example:
?
Thanks for the suggestion.
How would code using Dictionary objects this for example:
Code: Select all
Dim global1, global2, global3
...
global2 = "string2"
global3= "string3"
...
global1 = global2 & global3
Chris
Re: 1282 Simultaneous script execution corrupts variables
Code: Select all
Dim dict : Set dict = CreateObject("Scripting.Dictionary")
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Set SDB.Objects("MyDictionary") = dictDownload my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
Thanks, but surely still 'dict' may get corrupted by this problem. And that corruption will then get transferred to SDB.Objects.
Chris
Re: 1282 Simultaneous script execution corrupts variables
Sorry, maybe I should have given you a little more...
Code: Select all
Dim dict : Set dict = SDB.Objects("MyDictionary")
If Not (dict Is Nothing) Then
Exit Sub
End If
Set dict = CreateObject("Scripting.Dictionary")
Set SDB.Objects("MyDictionary") = dict
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
Still corruption can occur e.g.
Execution 1 will now find dict empty instead of containing the three strings.
Code: Select all
Execution 1: dict.Item("global3") = "string3"
Execution 2: Set dict = CreateObject("Scripting.Dictionary")
Execution 1: dict.Item("global1") = dict.Item("global2")&dict.Item("global3")Chris
Re: 1282 Simultaneous script execution corrupts variables
Theoretically, but unless your computer is one of those full room sized ones from the the 80s you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Re: 1282 Simultaneous script execution corrupts variables
And if it is at another point?? Or points?? e.g. being in a SUB called multiple times?? Or between the set and get of the variable are many lines of other code e.g. doing slow things like file I/O??trixmoto wrote:you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
I would like to hear from Jiri if he believes it is at all possible to use global variables without this risk of corruption.
Chris
Re: 1282 Simultaneous script execution corrupts variables
If you put it right at the beginning of each access point to your script then the script can only be running once within it's instance (as each subsequent to run will immediately exit out) and therefore there will be no corruption. Then at every exit point from your script you need to make sure that you set the SDB.Object back to "Nothing" so that the script then be run again.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.