Terminate vbscript after x minutes

前端 未结 4 1358
鱼传尺愫
鱼传尺愫 2021-01-14 03:09

I am working on a script with vbscript, and I would like it to terminate itself after x number of minutes.

I was thinking something like grabbing the time when the s

4条回答
  •  抹茶落季
    2021-01-14 04:00

    Here is another short and elegant solution which allows to terminate both the script and the external executable ran asynchronously, via WScript.Timeout

    Option Explicit
    
    Dim oSmallWrapperWshExec
    
    WScript.Timeout = 7
    Set oSmallWrapperWshExec = New cSmallWrapperWshExec
    ' Some code here
    MsgBox "Waiting timeout" & vbCrLf & vbCrLf & "You may close notepad manually and/or press OK to finish script immediately"
    
    Class cSmallWrapperWshExec
    
        Private oWshShell
        Private oWshExec
    
        Private Sub Class_Initialize()
    
            Set oWshShell = CreateObject("WSCript.Shell")
            With oWshShell
                Set oWshExec = .Exec("notepad")
                .PopUp "Launched executable", 2, , 64
            End With
    
        End Sub
    
        Private Sub Class_Terminate()
    
            On Error Resume Next
            With oWshShell
                If oWshExec.Status <> 0 Then
                    .PopUp "Executable has been already terminated", 2, , 64
                Else
                    oWshExec.Terminate
                    .PopUp "Terminated executable", 2, , 64
                End If
            End With
    
        End Sub
    
    End Class
    

提交回复
热议问题