Method to terminate script?

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Method to terminate script?

Post by chrisjj »

Is there a method call that will exit my script?

Like VBscript EXIT SUB but exiting the script rather than just the sub.
Chris
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Method to terminate script?

Post by Eyal »

There's the STOP statement but it only works in WScript, not VBscript.

I think the only way to do it is to voluntarily generate an error

Code: Select all

Err.Raise <Number>
or

Code: Select all

x = 10 / 0
It's convenient for debugging but not suitable for public release...
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
rivorson
Posts: 594
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Post by rivorson »

I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Post by chrisjj »

Eyal wrote:There's the STOP statement but it only works in WScript, not VBscript.

I think the only way to do it is to voluntarily generate an error

Code: Select all

Err.Raise <Number>
or

Code: Select all

x = 10 / 0
It's convenient for debugging but not suitable for public release...
Thanks. Trying it http://i.imgur.com/9o87Iw9.png (there's no <number> for script-special error) I find generates two dialog boxes, so is not as convenient as I'd like.
Chris
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Post by chrisjj »

rivorson wrote:I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.
That's the litter I already have and am seeking to eliminate.

Thanks anyway for the suggestion.
Chris
rivorson
Posts: 594
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Post by rivorson »

I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:

Code: Select all

Sub AllowEarlyExit()
    On Error Resume Next
    MainSub
    MsgBox "MainSub finished"
End Sub

Sub MainSub
    'some code
    Err.Raise 1 'throws error
    'some more code
End Sub
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Post by chrisjj »

rivorson wrote:I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:
...
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.
That will hide all the genuine error boxes too.

Is there some way to make the hiding specific to the user error? I can find no Error.Raise number for use error but perhaps it would be safe enough to repurpose an existing rare error number.
Chris
rivorson
Posts: 594
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Post by rivorson »

Error numbers can be between 0 and 65535. 0 to 512 are reserved for errors defined by VB. 513-65535 are available for user defined errors, presumably including errors defined by MM. I suggest raising the highest error number possible to terminate your code then check if there was an error that is smaller than that.

Code: Select all

Sub AllowEarlyExit()
    On Error Resume Next
    Dim ErrorNumber as Integer
    MainSub
    ErrorNumber = Err.Number
    If ErrorNumber > 0 And ErrorNumber < 65535 Then Err.Raise ErrorNumber
    MsgBox "MainSub finished"
End Sub

Sub MainSub
    'some code
    Err.Raise 65535 'throws error
    'some more code
End Sub
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Method to terminate script?

Post by Eyal »

If you don't already have VBScript Language Reference (VBSCRIP5.CHM), you can find its list of errors here:
https://msdn.microsoft.com/en-us/librar ... s.84).aspx
and On Error handling is explained a bit here: https://msdn.microsoft.com/en-us/librar ... s.84).aspx
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
Post Reply