2

Following an advice given to me in this question I have made this question. So I have an exe I want to run after the computer is idle (by idle I don’t mean sleep, I mean inactive or not being used) for sometime lets say 30 mins. Once the “inactive” timeout reaches I want the application to run. Saying that my PC is set up to only have the option “Screen sleep after “ in the Power options no Computer sleep, so the PC doesn’t go to sleep or the screen gets lock-screen unless I forced it using Windows+L keys, and I want to keep these settings as it is. I want to detect an inactivity time period and once the inactivity time period is reached then I want to execute an exe. Any idea how this can be done?

EDIT:

I've followed the steps provided in the answers:

Created a new task, added a trigger, set it to event: on Idle and created the task. In the conditionstab, specified the task waits 1min to become idle (instead of 10min just to test).

But then I wait for more than 10mins and the task is never triggered on PC1 but triggers on another PC2. I've checked online and found that PowerCfg -requests will show what is stopping the PC from being Idle and when I run it on PC2 nothing returns but when I run it on PC1 I get this "Legacy Kernel Caller" driver.

So I used Powercfg -requestsoverride but when I run powercfg -requestsoverride Driver "Legacy Kernel Caller" System , where it succeeds and I find it in the Powercfg -requestsoverride list, but then powercfg -requests it still shows [DRIVER] Legacy Kernel Caller under SYSTEM: and the task is never triggered any advice please?

Tak
  • 481
  • 2
  • 5
  • 20
  • So you want the application to prevent your system from locking? This isn’t a corporate machine is it? – Ramhound Jul 14 '20 at 06:15
  • @Ramhound No, the machine never locks unless I force locking it using windows+L keys. I want to detect an inactivity time period then when it reaches this inactivity time limit I run the exe. Is it clearer now? – Tak Jul 14 '20 at 06:17
  • Are you familiar with any programming language? It is easy if yes. – Bhavya Gupta Jul 14 '20 at 06:26
  • @BhavyaGupta Yes, C# – Tak Jul 14 '20 at 06:29

4 Answers4

5

What you want is easy. You can do it using task scheduler.

When creating a task, under Conditionstab, you have the option to start the task if computer is idle for: . In there just put how much idle time you want.

enter image description here

Just program the .exe you want to run, how often you want it and then use the idle condition.

Overmind
  • 9,924
  • 4
  • 25
  • 38
1

The far easiest solution is to use good old Task Scheduler.

Task Scheduler does not just allow you to set tasks on certain times dates, but it can actually also set tasks on certain events, such as at logon, and yes, also when idle.

Create a new task, add a trigger, set it to event: on Idle and create the task. In the conditionstab, you can specify how long the task waits to become idle (you set this to 30 minutes).

In addition, you can also automatically kill the task when the system becomes active again all from Task Scheduler.

LPChip
  • 59,229
  • 10
  • 98
  • 140
  • So this Idle time doesn’t mean computer being sleep? As mine never goes to sleep. – Tak Jul 14 '20 at 06:37
  • No, it means, no mouse movement nor key presses. – LPChip Jul 14 '20 at 06:41
  • I am thinking now, if the idle time reached and the exe started and then nothing happened for the same time idle time, this will relaunch the exe and will be running twice, any idea how to avoid that? – Tak Jul 14 '20 at 07:21
  • It will not. It goes into idle state and the task fires, and it will not fire it again until the computer goes out of idle state at least once, unless the app itself somehow makes the computer think its no longer idle. But setting up the task allows you to not check for idle time for a certain amount of time anyway. So Wait until the computer becomes idle for 30 minutes, and don't check idle time for 4 hours will basically say: once it reaches idle for 30 minutes, it will not check again until 4 hours have elapsed. – LPChip Jul 14 '20 at 08:22
  • could you explain with a screenshot in your answer how this can be done? Like this is my case in detail, I have an exe which opens a form for the computer user to fill, so if the idle time reached the form will open, now if a user after that moved the mouse or keyboard he will see this form and fill it and it will close, so if the computer becomes idle for x time again the form will open again and so on. – Tak Jul 14 '20 at 08:37
  • sorry I had to write another comment since chars limit. So Now this is the scenario I am not sure about what will happen, the idle time reached then then the form opens then an accidental event break idle time such as a user accidentally moving then mouse or keyboard but not filling the form as he didn’t use the computer, this means the form (exe) is still running, then the idle time reaches again, what will happen? Will the form (exe) runs and open again (twice)? I’ve upvoted your answer anyway :) – Tak Jul 14 '20 at 08:40
  • The task will trigger again, but you can specify what happens if the previous task has not yet quit. You can choose to run it in parallel, end the previous task, or ignore this one. – LPChip Jul 14 '20 at 09:43
  • How I can specify to end the previous task or ignore this one? – Tak Jul 14 '20 at 09:46
  • See the Settings tab – LPChip Jul 14 '20 at 10:02
  • I've tried now and set the settings "If the task is already running, then the following rule applies" to "Do not start a new instance" . But when I did this and when the windows application form is launched the first time, it still launches the second time although the windows application form is running from the first trigger. Any advice? – Tak Jul 14 '20 at 20:06
  • Does your windows form application launch a new instance after it starts? – LPChip Jul 14 '20 at 20:26
  • Yes. So the scenario is, if the task triggered when computer is idle for x time then my windows form application exe launches and stays shown until a user fill in some data and close it, but if the win form exe already launched and wasn't closed by user and the computer stayed idle again after x time then a new instance of my winform exe launches. – Tak Jul 14 '20 at 20:30
  • No, I mean, when your exe starts, does it call something else? Like for example, do you have a .bat or .cmd file that starts your exe with certain parameters? – LPChip Jul 14 '20 at 20:58
  • No, it doesn’t call anything, just waits for a user to fill in some details. – Tak Jul 14 '20 at 21:03
  • I was able to overcome this issue. Now I wonder why if I set the "start the task if the computer is idle" to 1min it doesn't start. What could be stopping it from starting? – Tak Jul 14 '20 at 22:12
  • I've updated my question so if you could check it please – Tak Jul 14 '20 at 23:21
  • I've posted another question here https://superuser.com/questions/1568707/task-scheduler-idle-task-not-triggered-windows-7 – Tak Jul 15 '20 at 00:32
  • Could you please check this: https://superuser.com/q/1568909/391215 – Tak Jul 16 '20 at 07:45
0

Sorry not in c# but is easy enough. This is a perfect job for AutoIt: http://autoitscript.com

Here's a script I threw together for you. Put it in an .au3 file, replace notepad with your exe, and for Run, include the full path:

#include <Timers.au3>
While 1
   Sleep(10)
   $idleTimer = _Timer_GetIdleTime()
   If $idleTimer > 60000 And Not ProcessExists("notepad.exe") Then
      Run("notepad.exe")
   ElseIf $idleTimer < 10 Then
      ProcessClose("notepad.exe")
   EndIf
WEnd

Reference - https://superuser.com/a/700647/1092861

Bhavya Gupta
  • 447
  • 3
  • 21
  • This will not work in my case as it relies on the idletime which means a computer being sleep, my computer never gets into an idle state and go to sleep, only the screen goes to sleep. – Tak Jul 14 '20 at 06:31
  • @Tak you are taking wrong definition for idle time. The computer goes to sleep after say 5 minutes of idle time. It means when no key is pressed or mouse moved. – Bhavya Gupta Jul 14 '20 at 06:33
0

You can use schtasks from command prompt to make the task easier:

schtasks /create /tn "My Task" /tr "EXE Application path" /sc onidle /i Number_Of_Minutes_Inactive
Wasif
  • 7,984
  • 2
  • 19
  • 32