在给定时间后自动终止 Windows 应用程序

Ale*_*sky 6 windows automation timer process task-scheduler

我正在寻找一种方法,让用户启动仅运行指定时间(例如一个小时)的 Windows 应用程序。一小时结束后,将弹出一个警报窗口,显示“程序将在五分钟内终止,除非您单击此处”。如果用户单击该按钮,程序将再运行一个小时,然后再次弹出警报。

如果没有简单的方法来完成此操作,那么我愿意让程序在给定时间后终止,而不弹出警告。

理想情况下,该方法应在 Windows XP 和 Windows 7 下运行。

小智 5

我可能会迟到,但你基本上可以通过使用命令提示符来完成

  1. 打开记事本
  2. 复制/粘贴此代码

    cd "PATH OF THE .EXE FILE"
    start APP.exe
    timeout /t 3600
    taskkill /im APP.exe /f
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将文件另存为 .bat

超时以秒为单位,要取消它只需关闭命令提示符


Ale*_*sky 3

我正在回答我自己的问题。嗯,实际上,专家交易所上的用户 BigBadWolf_000 准确而完整地回答了这个问题。我复制了他的 VBScript 代码如下:

Dim PromptTime, DelayTime, StrAppPath, AppExec, MsgTxt, intAnswer, intRet

Set objShell = CreateObject("WScript.Shell")

PromptTime = 60
DelayTime = 5
StrAppPath = "C:\windows\"
AppExec = "notepad.exe"
MsgTxt = "Do you want Notepad to close in 5 minutes?"

objShell.Run chr(34) & StrAppPath & AppExec & chr(34), 1, "False"
Do
    WScript.Sleep (1000 * 60 * PromptTime)
    intAnswer = Msgbox(MsgTxt, vbYesNo, "Please select Yes or No")
    If intAnswer = vbYes Then Exit Do
Loop

WScript.Sleep (1000 * 60 * DelayTime)
Set objWmi = GetObject("winmgmts:") 
Set objQResult = objWmi.Execquery("Select * from Win32_Process where name like '" & AppExec & "'")
For Each objProcess In objQResult 
intRet = objProcess.Terminate(1) 
Next 

Set objShell = Nothing
Set objWmi = Nothing 
Set objQResult = Nothing
Run Code Online (Sandbox Code Playgroud)

以下是 BigBadWolf_000 代码中包含的解释性注释:

The script below will do the following...
- lauch your application (app must be launched with this script)
- Promt you for action after an hour "Do you want YourApp to close in 5 minutes?" Yes/No
- Yes: Terminates all instances of the app after 5 minutes
- No: Restarts the clock and will prompt you again in an hour

The default PromptTime is set to 60 minutes and the terminate app DelayTime is set to 5 minutes.
You can change number for PromptTime = 60 and DelayTime = 5 to any number in minutes (minimum 1). The default app is set to Notepad so you can test.

Change the path of the app executable...
StrAppPath = "your path goes here, end with  \" 

Change the name of the app executable...
AppExec = "whatever.exe"

Change the popup message txt...withing the quotes
MsgTxt = "Do you want Notepad to close in 5 minutes?"

Copy and paste script to notepad and save as yourfilename.vbs. Double-click on the file to run or create a shortcut to it. Will work with Windows 7 and Windows XP SP3.
Run Code Online (Sandbox Code Playgroud)