unregister events not working [bug! or design?]

Download and get help for different MediaMonkey for Windows 4 Addons.

Moderators: Peke, Gurus

Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

unregister events not working [bug! or design?]

Post by Teknojnky »

example script;

Code: Select all

Sub OnStartup


  dim testbutton : set testbutton = sdb.ui.addmenuitem(sdb.ui.Menu_TBStandard,0,0)
  testbutton.caption = "Test"
  testbutton.usescript = script.scriptpath
  testbutton.onclickfunc = "testtoggle"
  


End Sub

Sub testtoggle(arg)

  if arg.checked = false then
    arg.checked = true
    script.registerevent sdb, "OnPlay", "TestOnPlay"
  Else
    arg.checked = false
    script.unregisterevents sdb
  End If

End Sub


Sub TestOnPlay

  msgbox("testonplay called")

End Sub

the script does not unregister the onplay event and each time you toggle it off/on it adds another call to the testonplay ...

meaning you will get multiple msgboxes for one onplay event..

what am I doing wrong, or is this broken?

using RC-3
Last edited by Teknojnky on Wed Jan 30, 2008 3:37 pm, edited 1 time in total.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

Well I guess that the unregister is not working, but I wouldn't suggest using it anyway as you'd break any other installed scripts which have events! :(

In this situation I usually register the "OnPlay" event in the "onStartup" sub and then toggle an object to use as a switch, something like...

Code: Select all

Sub OnStartup
  script.registerevent sdb, "OnPlay", "TestOnPlay"
  Set SDB.Objects("TestOnPlaySwitch") = Nothing

  dim testbutton : set testbutton = sdb.ui.addmenuitem(sdb.ui.Menu_TBStandard,0,0)
  testbutton.caption = "Test"
  testbutton.usescript = script.scriptpath
  testbutton.onclickfunc = "testtoggle" 
End Sub

Sub testtoggle(arg)

  if arg.checked = false then
    Dim itm : Set itm = SDB.NewSongData
    Set SDB.Objects("TestOnPlaySwitch") = itm
  Else
    Set SDB.Objects("TestOnPlaySwitch") = Nothing
  End If

End Sub


Sub TestOnPlay

  If Not (SDB.Objects("TestOnPlaySwitch") Is Nothing) Then
    msgbox("testonplay called")
  End If

End Sub
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.
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

Yea your probly right.. I was just wanting to avoid having event called if not needed, seems like waste of processing.
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

The plot thickens..

I remembered my 'stop after next' script, which utilized the on play event..

Code: Select all

'========================================================================== 
' 
' MediaMonkey Script 
' 
' NAME: Stop After Current 
' DESCRIPTION: adds 2 icons to the standard toolbar, to stop and pause 
'   after the current song finishes' 
' VERSION: 1.0 
' DATE  : July 16, 2007 
' AUTHOR: Teknojnky 
' 
' INSTALL: 
' - Copy script to MM directory scripts\auto 
' 
' TODO: 
' 
' 
'========================================================================== 
' 
Option Explicit 

'Public StopAfterTBB 
'Public PauseAfterTBB 
  
  
Sub OnStartUp() 

    Do While Not SDB.IsRunning
      SDB.ProcessMessages
    Loop


' add toolbar icons for pause after, and stop after, on the player toolbar 

' add separater' 
  SDB.UI.AddMenuItemSep SDB.UI.Menu_TbStandard,0,0 
  
' toggle pause button' 

  Dim PauseAfterTBB : Set PauseAfterTBB = SDB.UI.AddMenuItem(SDB.UI.Menu_TBStandard,0,0) 
  PauseAfterTBB.Caption = SDB.Localize("Pauses play after currently playing song") 
  PauseAfterTBB.OnClickFunc = "PauseAfter" 
  PauseAfterTBB.UseScript = Script.ScriptPath 
  PauseAfterTBB.IconIndex = 2 
  SDB.Objects("PauseAfterTBB") = PauseAfterTBB 

' toggle stop button' 
  
  Dim StopAfterTBB : Set StopAfterTBB = SDB.UI.AddMenuItem(SDB.UI.Menu_TBStandard,0,0) 
  StopAfterTBB.Caption = SDB.Localize("Stops play after currently playing song") 
  StopAfterTBB.OnClickFunc = "StopAfter" 
  StopAfterTBB.UseScript = Script.ScriptPath 
  StopAfterTBB.IconIndex = 3 
  SDB.Objects("StopAfterTBB") = StopAfterTBB 

End Sub 


Sub PauseAfter(PauseAfterTBB) 

  Script.UnRegisterAllEvents 

  If PauseAfterTBB.Checked = False Then 
    PauseAfterTBB.Checked = True 
    SDB.Objects("StopAfterTBB").Checked = False     
    Call Script.RegisterEvent(SDB, "OnPlay", "DoAfter") 
  ElseIf PauseAfterTBB.Checked = True Then 
    PauseAfterTBB.Checked = False 

  End If 
  
End Sub 


Sub StopAfter(StopAfterTBB) 

  Script.UnRegisterAllEvents 
  
  If StopAfterTBB.Checked = False Then 
    StopAfterTBB.Checked = True 
    SDB.Objects("PauseAfterTBB").Checked = False     
    Call Script.RegisterEvent(SDB, "OnPlay", "DoAfter") 
  ElseIf StopAfterTBB.Checked = True Then 
    StopAfterTBB.Checked = False 
  End If 
'     SDB.Player.Stop
End Sub 


Sub DoAfter()  
  Script.UnRegisterAllEvents 
  If SDB.Objects("PauseAfterTBB").Checked = True Then  
    SDB.Player.Pause 

  ElseIf SDB.Objects("StopAfterTBB").Checked = True Then 
    SDB.Player.Stop 
  End If 

msgbox ("did onplay event really unregister?")  

  SDB.Objects("PauseAfterTBB").Checked = False 
  SDB.Objects("StopAfterTBB").Checked = False     
End Sub 
I thought I would have to re-write the script based on the unregister not working for onplay..

but the above msgbox only appears once... meaning the event somehow got unregistered..

so I tried the script.UnregisterAllEvents in the test script..

Code: Select all


Dim testSDB : set testSDB = SDB

Sub OnStartup


  dim testbutton : set testbutton = sdb.ui.addmenuitem(sdb.ui.Menu_TBStandard,0,0)
  testbutton.caption = "Test"
  testbutton.usescript = script.scriptpath
  testbutton.onclickfunc = "testtoggle"
  


End Sub

Sub testtoggle(arg)

  if arg.checked = false then
    arg.checked = true
    script.registerevent testSDB, "OnPlay", "TestOnPlay"
  Else
    arg.checked = false
    script.unregisterevents testSDB
    set testSDB = Nothing
    
    script.UnregisterAllEvents
  End If

End Sub


Sub TestOnPlay

  msgbox("testonplay called")

End Sub

And still the testonplay was called.

so then I tried to create an object 'testSDB' = SDB.. that way I could unregister that testSDB and even set it to nothing.. and that did not work either..

so now, I have 2 different scripts which register and un register the same event and only one script works..

very odd..
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

OOoooooooooooooooooooooooooooooooooooooooooook

I have figured it out..

stop or pause after does 'script.unregisterallevents' within the event and on each of the sub's to toggle the buttons..

ONLY the EVENT sub will un register the onplay event..

when I change the test script:

Code: Select all


Dim testSDB : set testSDB = SDB

Sub OnStartup


  dim testbutton : set testbutton = sdb.ui.addmenuitem(sdb.ui.Menu_TBStandard,0,0)
  testbutton.caption = "Test"
  testbutton.usescript = script.scriptpath
  testbutton.onclickfunc = "testtoggle"
  


End Sub

Sub testtoggle(arg)

  if arg.checked = false then
    arg.checked = true
    script.registerevent SDB, "OnPlay", "TestOnPlay"
  Else
    arg.checked = false
    
  End If

End Sub


Sub TestOnPlay()

  msgbox("testonplay called you should get this once")
  
  script.UnregisterEvents SDB

End Sub

to unregister SDB within the event, it appears to work..


I don't know if this is limited to only SDB events or it affects all, and I don't know whether it is intentional or not (bug!)... but it is definately inconsistent and confusing behavior...
jiri
Posts: 5429
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri »

Ok, I see what's going on. The thing is that the old way of calling events (i.e. using 'onclickfunc') actually starts another script instance everytime the event is called. Then if you call UnregisterEvents in 'Sub testtoggle', MM doesn't know what you actually want to unregister, this script instance hasn't registered any event yet!

The easiest fix would be to not use

Code: Select all

testbutton.usescript = script.scriptpath
testbutton.onclickfunc = "testtoggle"
but rather use RegisterEvent for OnClick event. Then the script will always remain loaded in memory and there won't be any problem regarding events.

Jiri
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

Thanks for the clarification Jiri.

Perhaps, when time permits, the wiki could be updated to indicate such 'deprecated' functions and pointers to the preferred ones along with any other relevant issues/changes that might be need to be considered by the scripter.
Soren Werk
Posts: 29
Joined: Sat Jan 12, 2008 1:24 pm
Location: Denmark
Contact:

Yes - that clarifies things!

Post by Soren Werk »

Yes - that clarifies things!

And it helped me quite a bit with my PauseBefore script.

I guess, since the MM wiki is a wiki, we could all do our part to keep it up-to-date. I'll add my newfound knowledge soonest - even if I am a newbie.
Last edited by Soren Werk on Fri Feb 01, 2008 2:37 am, edited 1 time in total.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

Yes, that's the idea of a wiki - everyone can share their knowledge freely! :)
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.
Soren Werk
Posts: 29
Joined: Sat Jan 12, 2008 1:24 pm
Location: Denmark
Contact:

Post by Soren Werk »

Turns out the answers are allready in the wiki! See Tips & Tricks - Event mechanisms.

But I had used a script that did not use the new event mechanism as a basis for my PauseBefore script - which now does.
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

Good Tips link, I had missed that or forgotten about it. Some good info there.
Post Reply