This project has moved. For the latest updates, please go here.

How to exit the vbscript code at runtime

Jul 20, 2015 at 7:32 PM
Hi,

As we are able to exit the vbscript at runtime in Windows scripting Host by using:
WScript.Quit

How can we exit the script at run time in clearscript?

Regards
Jaskaran
Coordinator
Jul 21, 2015 at 1:18 PM
Hi Jaskaran,

The WScript.Quit method simply exits the process, and you can easily expose a similar API with ClearScript:
engine.Script.Quit = new Action<int>(Environment.Exit);
On the other hand, if you're looking for a way to terminate the current script without exiting the process, you can call ScriptEngine.Interrupt():
engine.Script.Quit = new Action(() => ScriptEngine.Current.Interrupt());
Note that doing so will generate a ScriptInterruptedException that you'll have to catch.

Another possibility might be to have your Quit API throw a custom exception. The problem there is that the script can disable error propagation via On Error.

Good luck!
Jul 21, 2015 at 3:34 PM
Thanks for the reply.

Yes, I am trying to terminate the current script without exiting the process.

While using ScriptEngine.Interrupt(): a popup with following message appears:
"Script execution interrupted by host"

I want to terminate the script silently without displaying any popup.

Is this possible?

Regards
Jaskaran
Coordinator
Jul 21, 2015 at 4:06 PM
Hi again,

It sounds like you're letting the ScriptInterruptedException propagate out to a handler that displays the error message in a popup. To suppress that behavior, simply catch the exception:
engine.Script.Quit = new Action(() => ScriptEngine.Current.Interrupt());
try {
    engine.Execute(codeThatCallsQuit);
}
catch (ScriptInterruptedException) {
}
Cheers!
Jul 21, 2015 at 4:31 PM
Hi,

Unfortunately i am not able to suppress the popup displaying "Script execution interrupted by host" :

I have tried following codes:

Code1:
        try
        {
           Script.engine.Interrupt(); 

        }
        catch (ScriptInterruptedException)
        {
        }

Code2:
(Unable to compile : Says "Microsoft.ClearScript.ScriptEngine does not contain defination for current)
engine.Script.Quit = new Action(() => ScriptEngine.Current.Interrupt());
try {
engine.Execute(util.Quit);
}
catch (ScriptInterruptedException) {
}

please help!
Coordinator
Jul 21, 2015 at 5:46 PM
Hi Jaskaran,

If ScriptEngine.Current is undefined, you're using a relatively old version of ClearScript, and we recommend that you switch to the latest version.

In any case, try the following in a console application:
engine.Script.Quit = new Action(() => engine.Interrupt());
engine.AddHostType("Console", typeof(Console));

try {
    engine.Execute(@"
        call Console.WriteLine(""Before"")
        call Quit
        call Console.WriteLine(""After (shouldn't get here)"")
    ");
}
catch (ScriptInterruptedException) {
}
This code should work with older versions of ClearScript.

Thanks!
Jul 22, 2015 at 4:56 AM
Thanks for the solution.
Also i have updated the clearscript version.