Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Run task on unlock after certain period of time


29 May 2013   #1

Windows 7 Professional 32bit
 
 
Run task on unlock after certain period of time

My computer is set to automatically lock after 10 minutes of user idle. I want to run a task when I log on, but only if the machine has previously been idle for an hour. This is so the task only runs when I've actually been away from the computer, not just if it's timed out. Is this possible?

My System SpecsSystem Spec
.

30 May 2013   #2

Windows 7 Home Premium 64bit
 
 

1. Have you tried task scheduler - create task -... triggers - new -begin .. - on WS unlock
Then once it is setup disable it. But you would have to enable it if you knew how long the machine would be unattended.
I did not see any option for the task to run only if idle for 1 hour though.

2. Or you could not use task scheduler - just set up a .bat file and run it manually as needed.
Welcome to the best Windows Forum on the Web!

I have a question, has anyone ever stumped the "panel" here at seven forums?
I wish I had joined here long ago!
My System SpecsSystem Spec
30 May 2013   #3

W7 Pro SP1 64bit
 
 

Like Josea mentioned, welcome to the Seven Forums and you can use a scheduled task to run the app of interest when you unlock the computer. The only way that I can think of to accomplish the idle time condition is to use a scripting tool like AutoIt3. You will need to write your own script. You can search (join?) the AutoIt forums for help or I might be able to help a bit in these forums.

Have your script start when you boot the computer. The script tracks idle time. If that idle time exceeds 60 minutes, then have the script create a simple text file.

Then - have the scheduled task run a batch file upon WS unlock.

Have the batch file run your app only if the file exists.
Code:
IF EXIST c:\myfolder\idle.txt myapp.exe
Then have the batch file remove the simple text file.

Or - depending on what the app does that you want to run - you can let the script start it after the 60 minutes of idle time and even if the computer is locked.
My System SpecsSystem Spec
.


30 May 2013   #4

W7 Pro SP1 64bit
 
 

@Josea,

By the way, I left my browser open to this thread for for many hours while attempting to come up with a solution. It was not until you posted about the scheduled task trigger of workstation unlock that the pieces for what I was wanting to post came together. I don't use scheduled tasks much and I had no idea that unlocking the computer could be the trigger. There probably is a pure AutoIt solution, but off of the top of my head, I don't know how to reliably have AutoIt detect when the workstation has been unlocked.

I hope that the OP can make use of our combined efforts.
My System SpecsSystem Spec
30 May 2013   #5

Windows 7 Professional 32bit
 
 

That's a crazily impressive solution, thanks both of you for your input! It's a little more complicated than I was hoping (as I want to put it on a few computers if successful), but nothing I can't work with.

So far I've got an AutoIt script which is creating the idle file (on demand), an AutoIt script which is running the batch file hidden (successful), and the batch file running the program and deleting the idle file (successful). I then ran out of time so will get back to it later today.

What I still need to do is improve the idle file script so that it runs on startup and then checks for idle every 10 minutes (currently only checks once when I run it). I'll then setup the task which runs the hidden batch file script to run every 10 minutes as well. Between the two of them it should be able to do what I'm trying to do.

I'll post again once I've had another go at it.
My System SpecsSystem Spec
30 May 2013   #6

W7 Pro SP1 64bit
 
 

It sounds like your requirements have changed a bit... and that is fine.

Below is one way to do what you originally asked about doing:

Code:
AutoItSetOption ("TrayIconDebug", 1)

;prevents multiple copies of the script from running
$S_running = "idle-timer-test" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)

;you can remark this out too - you always want the script to run
HotKeySet("+{ESC}", "_end_this") ; press Ctrl-ESC to end script

#include <Timers.au3>
#include <File.au3>

;delete the file if it happens to be there
FileDelete(@MyDocumentsDir & "\idle-time.txt")

While 1
    Local $iIdleTime = _Timer_GetIdleTime()
    ;the tray tip is just for debugging - it can be remarked out
    TrayTip('press Shift+ESC to end script', "been idle for about " & $iIdleTime & "ms", 100)
    Sleep(1000)
    If $iIdleTime > (1000 * 60) Then ;add another times 60 to make it one hour
        _FileCreate(@MyDocumentsDir & "\idle-time.txt")
        Do
            ;the tray tip is just for debugging - it can be remarked out
            TrayTip('press Shift+ESC to end script', "waiting for idle-time.txt to be deleted", 100)
            Sleep(1000)
        Until Not FileExists(@MyDocumentsDir & "\idle-time.txt")
    EndIf
WEnd

Func _end_this()
    Exit
EndFunc
Below is the batch file to be called by unlocking the computer:

Code:
if exist "C:\Users\username\Documents\idle-time.txt" notepad.exe
del "C:\Users\username\Documents\idle-time.txt"
pause
pause can be removed once it works

Maybe you can glean something from the code above.

Below the second video in this post is some info about AutoIt that you might want to keep in mind.
My System SpecsSystem Spec
31 May 2013   #7

Windows 7 Professional 32bit
 
 

Quote   Quote: Originally Posted by UsernameIssues View Post
It sounds like your requirements have changed a bit... and that is fine.

Below is one way to do what you originally asked about doing:

Code:
AutoItSetOption ("TrayIconDebug", 1)

;prevents multiple copies of the script from running
$S_running = "idle-timer-test" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)

;you can remark this out too - you always want the script to run
HotKeySet("+{ESC}", "_end_this") ; press Ctrl-ESC to end script

#include <Timers.au3>
#include <File.au3>

;delete the file if it happens to be there
FileDelete(@MyDocumentsDir & "\idle-time.txt")

While 1
    Local $iIdleTime = _Timer_GetIdleTime()
    ;the tray tip is just for debugging - it can be remarked out
    TrayTip('press Shift+ESC to end script', "been idle for about " & $iIdleTime & "ms", 100)
    Sleep(1000)
    If $iIdleTime > (1000 * 60) Then ;add another times 60 to make it one hour
        _FileCreate(@MyDocumentsDir & "\idle-time.txt")
        Do
            ;the tray tip is just for debugging - it can be remarked out
            TrayTip('press Shift+ESC to end script', "waiting for idle-time.txt to be deleted", 100)
            Sleep(1000)
        Until Not FileExists(@MyDocumentsDir & "\idle-time.txt")
    EndIf
WEnd

Func _end_this()
    Exit
EndFunc
Below is the batch file to be called by unlocking the computer:

Code:
if exist "C:\Users\username\Documents\idle-time.txt" notepad.exe
del "C:\Users\username\Documents\idle-time.txt"
pause
pause can be removed once it works

Maybe you can glean something from the code above.

Below the second video in this post is some info about AutoIt that you might want to keep in mind.
Nope you're right on the money! I didn't have my head around getting the constantly updating idle time so I was thinking I would have to call on a regular basis and that it would be easier to get a script to call on a schedule, but your script was much better. I had only got as far as get idle time and create file if greater than an hour (didn't want to just come out and ask for the script without giving it a go first). I had the batch working but that was the easy bit.

In conclusion:

On startup the idle-timer-test script (see above) runs. This virtually constantly calls the idle time from Windows. When this exceeds one hour the script creates a file in My Documents and then waits until that file is deleted before getting the idle time again.

When I unlock, the batch file checks to see if the idle time file exists. If so the batch file runs the program and then deletes the file so that the idle script starts getting the idle time again. If the file doesn't exist then the batch files does nothing.

The only other thing I did was wrap the batch file in an AutoIt script
Code:
Run("check-run-notepad.bat", "", @SW_HIDE)
and have that run on unlock (instead of the batch file itself) so that the batch file actions were invisible. There's probably an easier way to do this but since I was playing with AutoIt anyway ... great piece of software!

Thanks for your efforts with this, much appreciated!
My System SpecsSystem Spec
31 May 2013   #8

W7 Pro SP1 64bit
 
 

You are welcome.

I was just curious about how such a script would look so I put that code together.

Calling the batch file via a second AutoIt script so that the cmd window is hidden is fine, but you might want to explore having that second AutoIt script replace the batch file:
Code:
If FileExists(@MyDocumentsDir & "\idle-time.txt") Then
    Run("your app here")
EndIf
Or, AutoIt supports single line If statements where you do not need the EndIf:
Code:
If FileExists(@MyDocumentsDir & "\idle-time.txt") Then Run("your app here")
So your second AutoIt script could replace the batch file using about the same one line of code.
My System SpecsSystem Spec
Reply

 Run task on unlock after certain period of time




Thread Tools



Similar help and support threads for2: Run task on unlock after certain period of time
Thread Forum
BSOD any game for a period of time (hal.dll & ntoskrnl.exe). BSOD Help and Support
'Please Wait' time period during theme change Themes and Styles
Monitor turn off after long period time Performance & Maintenance
BSOD When leaving my PC on for a longer period of time BSOD Help and Support
BSOD when idle for a long period of time. BSOD Help and Support
Multiple BSoD over a period of time. BSOD Help and Support
PC slow's down a lot after a period of time Performance & Maintenance

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 10:25 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33