dcsimg
Results 1 to 8 of 8

Thread: Stop script control from running

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    116

    Stop script control from running

    I can't believe that there is no function inside the script control to make it stop executing.
    It has a timeout in ms which doesn't do anything at all and I don't really want a timeout anyway because I really don't know lhow long a certain script might execute. What I really need is just a way to make the script stop from within VB6. The first thing I tried was [scriptobject].Reset and that returned "Can not call this method while the script is executing". How do I stop the script, then? If there is really no way, this is ridiculous. How can the host application not have control over a bad script that might be running?

  2. #2
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    306

    Re: Stop script control from running

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:05 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    116

    Re: Stop script control from running

    This would work if the code is entered by the programmer, but I don't know how I would inject that "throw ""stopped""" into a user's script that was running. It seems impossible.

    There has to be a way to do this. Other than this, the script control is working fine for me. I don't want to have to find another script solution when this one works well except for this one fatal flaw.

    Are there any other controls that expose or wrap the windows script host system?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    116

    Re: Stop script control from running

    Well, maybe I got closer

    If I create the object at runtime using Set x = CreateObject("MSScriptControl.ScriptControl")
    then when I want to stop it I use: Set x = Nothing

    This works, but only once. The second time I run the script, it executes two instances of it and crashes when I try to stop it the second time.

    Is there a nicer way to unload this object? Maybe there is a way to execute it in a separate process?

    I can't find any other account of this problem and I'm surprised that no one else has needed to stop a script before. The main application should be in full control and the way this works, it looks like the script is in control. I don't see how anyone could ever use this object in a professional application as bad user scripts could cause major problems if they can't be stopped.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    116

    Re: Stop script control from running

    OK, so my temporary solution is an adjunct of what dz32 posted:

    Code:
    Dim engine
    Dim isRunning As Boolean
    
    Private Sub btnRun_Click()
    If isRunning Then
        MsgBox "The script is currently executing. Please press stop first.", vbExclamation, "Running"
        Exit Sub
    End If
    
    If Not InsertBreaks Then Exit Sub
    
    Set engine = CreateObject("MSScriptControl.ScriptControl")
    engine.Language = "VBScript"
    engine.AllowUI = True
    engine.Reset
    isRunning = True
    ....
    ....
    ....
    End Sub
    
    Private Sub btnStop_Click()
    engine.AddCode "abort = 1"
    isRunning = False
    End Sub
    
    Private Function InsertBreaks() As Boolean
    If InStr(1, syntax.Text, "while", vbTextCompare) <> 0 Then
        If InStr(1, syntax.Text, "do while", vbTextCompare) = 0 Then
            MsgBox "You must use do while instead of while. While is not supported in this engine.", vbExclamation, "Do While"
            InsertBreaks = False
            Exit Function
        End If
    End If
    If InStr(1, syntax.Text, "if abort = 1 then exit do", vbTextCompare) = 0 Then
        syntax.Text = Replace(syntax.Text, "loop", "if abort = 1 then exit do" & vbCrLf & "loop", 1, -1, vbTextCompare)
    End If
    If InStr(1, syntax.Text, "if abort = 1 then exit for", vbTextCompare) = 0 Then
        syntax.Text = Replace(syntax.Text, "next", "if abort = 1 then exit for" & vbCrLf & "next", 1, -1, vbTextCompare)
    End If
    InsertBreaks = True
    End Function

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,206

    Re: Stop script control from running

    The timeout facility only works within the script engine's interpreter on its own statements.

    If you allow unsafe operations like CreateObject all bets are off. The script can invoke methods on created objects that will be immune to interruption.

    The typical way you deal with this is to disallow unsafe scripting. The main program would provide objects to the script which contain checks of some global "abort flag" as part of their properties and methods.

    Here's an excerpt from a "WScriptSim" class, an instance of which is provided to the script:

    Code:
    Public Sub Quit(Optional ByVal Status As Long)
        Err.Raise gINTERNAL_ERROR 'Internal error, catch this in Script Control Error event!
    End Sub
    
    Public Sub Sleep(ByVal Milliseconds As Long)
        DoEvents
        If Milliseconds > 0 Then SleepAPI Milliseconds
        If gblnAbort Then
            gblnAbort = False
            Err.Raise gINTERNAL_ERROR 'Abort script.
        End If
    End Sub
    Here is where I add it:

    Code:
        Set WScript = New WScriptSim
        With scScript
            .SitehWnd = hWnd
            .AddObject "WScript", WScript, False
            .Timeout = lngTimeout
    ...

  7. #7
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    306

    Re: Stop script control from running

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:05 AM.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    306

    Re: Stop script control from running

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width