Preserving Variables and Not Re-Executing Code

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Preserving Variables and Not Re-Executing Code

Re: Preserving Variables and Not Re-Executing Code

by CarlitoGil » Tue Oct 02, 2012 2:55 am

I appreciate it, but I'll keep adding this

Code: Select all

Set DummyTimer = SDB.CreateTimer(0)
Script.RegisterEvent DummyTimer, "OnTimer", ""
DummyTimer.Enabled = False
It's a hack, but doesn't change the code to work, and performance wise, I think having the global variables is faster, and better looking than accessing them on SDB.Objects("Diccionary").Item("Variable") many times.
Also, is easier to share code between an Auto Script and a Search Script.
The Search Scripts keep the variables alive for the active script until the web search window is closed.

I should have put this on Wishlist, they could add something to SDB.Tools to do what my DummyTimer does.
It seems that if a large number of your scripts have to do this, it's something needed.

Re: Preserving Variables and Not Re-Executing Code

by trixmoto » Tue Oct 02, 2012 2:33 am

Personally I create a dictionary object and store it in SDB.Objects, then I store all my global variables as items within that dictionary. A large number of my scripts use this mechanism. And if the global variable you want to hold is an object, then yes, the way to do this is to also add this to SDB.Objects, that's what it's there for.

Re: Preserving Variables and Not Re-Executing Code

by CarlitoGil » Tue Oct 02, 2012 12:45 am

Eyal wrote:By "ending the session" I meant ending MediaMonkey. Global variables will retain their values until then.
Well, I misunderstood your meaning of "ending the session".
Global variables will not retain their values until ending MM.

Code: Select all

Option Explicit

Sub OnStartUp()
	Dim Menu

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST1"
	Menu.Caption = "TEST 1"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST2"
	Menu.Caption = "TEST 2"

End Sub

Dim KeepVariable

Sub TEST1(Menu)
	KeepVariable = False
	msgbox("TEST 1")
	' session "ends"
End Sub

Dim Test2Timer
Sub TEST2(Menu)
	msgbox("TEST 2")
	msgbox("alive? "& KeepVariable)
	KeepVariable = True
	
	' keep session alive
	Set Test2Timer = SDB.CreateTimer(0)
	Script.RegisterEvent Test2Timer, "OnTimer", ""
	Test2Timer.Enabled = False
End Sub

The variable that I need to keep alive has an object, so an INI won't help.
In my tests, if an object is not assigned to SDB.Objects after the script "ends", it's lost.
Unless, of course, I prevent the script from "ending" by registering a dummy timer.
This object is the one that would trigger the event that calls a sub for the script to continue, if it dies, no trigger.

There are many global variables being shared by multiple subs, and the event could be triggered thousand of times, very quickly. so saving and loading every time would be too messy.
Eyal wrote:it's not a good idea (it's against structural programming) to have code outside of subs or functions (though variables are OK).
I did not say anything about code outside of subs or functions.

Re: Preserving Variables and Not Re-Executing Code

by Eyal » Tue Oct 02, 2012 12:09 am

By "ending the session" I meant ending MediaMonkey. Global variables will retain their values until then.

Secondly, it's not a good idea (it's against structural programming) to have code outside of subs or functions (though variables are OK).

You can also use ini or registry functions to keep variables value outside of sessions, but I don't think it's what you want to do.

Re: Preserving Variables and Not Re-Executing Code

by CarlitoGil » Mon Oct 01, 2012 11:48 pm

Let's say you don't want to end the session just yet, and if you register a timer to resume the script in a little while, it won't end.
MM doesn't end the session if a timer is registered.

But I don't actually need a timer, there is an event that's going to resume the script when it's needed.
So, I want to remove the timer without ending the session.

Re: Preserving Variables and Not Re-Executing Code

by Eyal » Mon Oct 01, 2012 11:41 pm

What exactly is your goal?
GIL wrote:How to preserve global variables
Global variables are defined outside of subs, usually before first Sub. They retain their values until session ends.

Preserving Variables and Not Re-Executing Code

by CarlitoGil » Sun Sep 30, 2012 12:49 pm

Hello, I've prepared a script to demonstrate a question I have:
How to preserve global variables and not execute code outside subs everytime a sub is called, without sleeping or timers?

Code: Select all

Option Explicit

Sub OnStartUp()
	Dim Menu

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST1"
	Menu.Caption = "TEST 1"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST2"
	Menu.Caption = "TEST 2"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST3"
	Menu.Caption = "TEST 3"
	
End Sub

msgbox("GLOBAL")

Sub TEST1(Menu) ' the "GLOBAL" message is shown everytime
	msgbox("TEST 1")
End Sub

Dim Test2Timer
Sub TEST2(Menu) ' the "GLOBAL" message is shown only the first time
	msgbox("TEST 2")
	
	Set Test2Timer = SDB.CreateTimer(1000)
	Script.RegisterEvent Test2Timer, "OnTimer", "Test2Timeout"
	Test2Timer.Enabled = True
End Sub

Sub Test2Timeout(Source)
	Test2Timer.Enabled = False
	msgbox("TEST 2 TIMEOUT")
End Sub

Dim Test3Timer
Sub TEST3(Menu) ' the "GLOBAL" message is shown the first time and when the timer is destroyed
	msgbox("TEST 3")
	
	Set Test3Timer = SDB.CreateTimer(1000)
	Script.RegisterEvent Test3Timer, "OnTimer", "Test3Timeout"
	Test3Timer.Enabled = True
End Sub

Sub Test3Timeout(Source)
	Test3Timer.Enabled = False
	Script.UnregisterEvents Test3Timer
	Set Test3Timer = Nothing
	msgbox("TEST 3 TIMEOUT")
End Sub
TEST 3 only shows the "GLOBAL" message the first time and when there are no timers.
I'd like a script to behave like TEST 3 without using a timer, is it posible?

Top