Method to terminate script?

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

Moderator: Gurus

Method to terminate script?

Postby chrisjj » Fri Oct 14, 2016 4:51 pm

Is there a method call that will exit my script?

Like VBscript EXIT SUB but exiting the script rather than just the sub.
Chris
chrisjj
 
Posts: 4926
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Postby Eyal » Thu Oct 20, 2016 1:36 am

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 ].
Eyal
 
Posts: 3099
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Method to terminate script?

Postby rivorson » Thu Oct 20, 2016 3:47 am

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.
rivorson
 
Posts: 526
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Postby chrisjj » Thu Oct 20, 2016 5:07 am

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: 4926
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Postby chrisjj » Thu Oct 20, 2016 5:08 am

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
chrisjj
 
Posts: 4926
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Postby rivorson » Thu Oct 20, 2016 7:11 am

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.
rivorson
 
Posts: 526
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Postby chrisjj » Thu Oct 20, 2016 12:31 pm

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
chrisjj
 
Posts: 4926
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Method to terminate script?

Postby rivorson » Thu Oct 20, 2016 1:17 pm

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
rivorson
 
Posts: 526
Joined: Thu Jul 25, 2013 4:17 am

Re: Method to terminate script?

Postby Eyal » Thu Oct 20, 2016 1:42 pm

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/library/2z84dwk8(v=vs.84).aspx
and On Error handling is explained a bit here: https://msdn.microsoft.com/en-us/library/53f3k80h(v=vs.84).aspx
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
Eyal
 
Posts: 3099
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec


Return to MediaMonkey 4 Addons developer forum

Who is online

Users browsing this forum: No registered users and 2 guests