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

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

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

如果没有简单的方法来实现这一点,那么我愿意让程序在给定时间后终止,而不弹出警告。

理想情况下,该方法应该在 Windows XP 和 Windows 7 下都有效。

答案1

我可能来晚了,但你基本上可以使用命令提示符来完成

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

    cd "PATH OF THE .EXE FILE"
    start APP.exe
    timeout /t 3600
    taskkill /im APP.exe /f
    
  3. 将文件另存为 .bat

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

答案2

我在回答我自己的问题。实际上,这个问题已经由用户 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

以下是 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.

答案3

您可以使用自动识别为此编写的脚本。

相关内容