5

I'm looking for a way for a user to launch a Windows application that will only run for a given amount of time, say an hour. After the hour is up, an alert window will pop up, saying "Program will be terminated in five minutes, unless you click here". If the user clicks on the button, the program will run for another hour, after which the alert will pop up again.

If there is no easy way to accomplish this, then I'm willing to have the program simply terminate after a given time, without the pop-up warning.

Ideally, the method should work under both Windows XP and Windows 7.

2
  • I believe the most reliable and perhaps simplest way is to write a program to do it. You might be able to achieve the same thing with batch files and schedule task, but it's actually more complicated than programming...
    – billc.cn
    Nov 6, 2011 at 14:42
  • @billc.cn: Thanks, but can you provide a little more guidance? Are you referring to a scripting language? In the end, I need some simple way for this to be implemented on workstations, where the end-users can just double-click on an icon to launch the time-limited application. Thanks. Nov 6, 2011 at 15:47

3 Answers 3

4

I might be late here but you can basically do it by using command prompt

  1. Open notepad
  2. Copy/paste this code

    cd "PATH OF THE .EXE FILE"
    start APP.exe
    timeout /t 3600
    taskkill /im APP.exe /f
    
  3. Save the file as .bat

the timeout is in seconds, to cancel it just close the command prompt

1
  • That works well, i had to make a small change with [start "title" "C:\path\program.exe"]
    – ToughPal
    Mar 19, 2020 at 16:08
3

I'm answering my own question. Well, actually, the question was answered accurately and completely by user BigBadWolf_000 on Experts Exchange. I'm copying his VBScript code below:

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

Here are the explanatory comments included with BigBadWolf_000's code:

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.
2

You can use AutoIt script for this.

1
  • 1
    Thanks, AutoIt looks promising. I was hoping that there would be some easier way to achieve my goal, but it looks like I may have to learn AutoIt. It may come in handy for other uses, though. Nov 6, 2011 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .