代码之家  ›  专栏  ›  技术社区  ›  Iain Fraser

如何让HTA重新启动?

  •  2
  • Iain Fraser  · 技术社区  · 15 年前

    • 用户单击“注销”或如果5分钟内没有输入
    • 删除internet临时文件
    • 关闭HTA
    • 重新打开HTA

    我考虑过的一件事是:在关闭应用程序之前,设置一个只运行一次的调度任务以再次运行HTA。但我不认为这是一个非常优雅的解决方案:(

    干杯

    伊恩


    2 回复  |  直到 15 年前
        1
  •  3
  •   Todd    15 年前

    当注销或超时时,HTA运行一个重新启动脚本,然后退出。重新启动脚本等待MSHTA进程结束,然后重新启动它并退出自身。

    这可以通过使用WMI扫描正在运行的进程并查找MSHTA.EXEwho的命令行参数包含HTA的名称。

    下面是一个小例子来说明这一点。

    <html> <!-- RestartTest.hta -->
    <head>
    <title>Self Restarting HTA Test</title>
    
    <script language="VBScript">
    sub RunApp(sApp)
      CreateObject("WScript.Shell").Run(sApp)
    end sub
    sub LogOff()
      RunApp "cscript.exe RestartHTA.vbs"
      MsgBox "simulating delayed exit - click to close"
      window.close()
    end sub
    </script>
    </head>
    
    <body >
    <input type="button" value="Log off" onClick="LogOff()" />
    </body>
    </html>
    

    这是重新启动脚本,另存为重新启动.vbs:

    ' RestartHTA.vbs'
    ' wait until RestartTest.hta is not running and restart it.'
    
    MSHTA = "mshta.exe"
    sHTA = "RestartTest.hta"
    
    say "waiting for process " & MSHTA & " with param " & sHTA & " to end"
    secs = 2000
    
    bRunning = vbTrue
    do while bRunning
      if ProcessRunning(MSHTA, sHTA) then
        say "still running, wait a few seconds"
        WScript.Sleep secs
      else
        bRunning = vbFalse
      end if
    loop
    
    say "HTA not found, proceding to restart"
    WScript.Sleep secs
    
    CreateObject("WScript.Shell").Run(sHTA)
    WScript.Quit
    
    '---'
    
    function ProcessRunning(sProcess, sParam)
      set oWMI = GetObject( _
        "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
      set cProcs = oWMI.ExecQuery( _
        "select * from Win32_Process where Name = '" & sProcess & "'")
    
      bFound = vbFalse
      for each oProc in cProcs
        say oProc.Name & ": " & oProc.CommandLine
        if (InStr(oProc.CommandLine, sParam) > 0) then
          say "found"
          bFound = vbTrue
        else
          say "wrong param"
        end if
      next
    
      ProcessRunning = bFound
    end function
    
    sub Say(s)
      WScript.Echo s
    end sub
    
    '==='
    

    你可以运行任何一个启动循环,然后关闭HTA的X按钮打破它。请注意,我使用CScript显式运行重新启动脚本。这只是为了演示它的控制台输出(因为某些原因,我在设置和保持CScript为我的机器的默认值时遇到了困难)。

        2
  •  2
  •   omegastripes    9 年前

    请看下面的代码:

    <html>
        <title>HTA Restart</title>
        <head>
        <HTA:APPLICATION
            Id="oHTA"
            Border="thick"
            InnerBorder="no"
            Scroll="no"/>
        </head>
        <script language="vbscript">
            Sub Restart()
                CreateObject("WScript.Shell").Run "mshta " & oHTA.commandLine
                window.close
            End Sub
        </script>
        <body style="background-color: buttonface;">
            <input type="button" value="Logout" onclick="Restart()" />
        </body>
    </html>