Author Topic: Execute only when IDLE  (Read 1556 times)

Botattack

  • Newbie
  • *
  • Posts: 3
Execute only when IDLE
« on: July 18, 2019, 03:50:30 AM »
Is there any way to delay the execution of a command for a certain app until the the app is idle.
I am not working with games but with certain architecturals programms.

Sometimes the programm needs to update different things so I have to wait until the process is finish to continue.
It is obvious that even the shortcuts keys do not work

I am wondering if it is possible somehow to delay any command until the process is completed.
There is no certain time of delay so voiceattack has to understand when the process is completed to execute the command.

Thank you

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4786
  • RTFM
Re: Execute only when IDLE
« Reply #1 on: July 18, 2019, 11:07:16 AM »
Is there any indication of when the application is in this non-responsive state aside from lack of response?

A popup window would be ideal, as it allows monitoring for its existence.


Otherwise, you'd probably have to resort to checking the CPU usage of the process to attempt to deduce whether it's busy or not, but you'd have to have an algorithm that checks it over time so it doesn't trigger off every random CPU usage spike.

Botattack

  • Newbie
  • *
  • Posts: 3
Re: Execute only when IDLE
« Reply #2 on: July 19, 2019, 01:40:31 AM »
In almost all the update cases there is a window poped up that says "update" or "checking".
The windows pops up but sometimes it disaapears earlier than the process's end.

This window does not start a new process on the task manager . Just the CPU percentage increases.

Is there anything that can be done to dealy the command execution when this is happening?

Thanks for the answer.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4786
  • RTFM
Re: Execute only when IDLE
« Reply #3 on: July 19, 2019, 01:27:12 PM »
You could try something like this:

Code: [Select]
Set Text [~~windowTitle] to 'update / checking window title'
Start Loop While : [{WINDOWEXISTS:~~windowTitle}] Equals '1'
End Loop
Set Text [~~targetProcess] to 'process name'
Set decimal [~~appCPU] value to 100,00000
Start Loop While : [~~appCPU] Is Greater Than 5,00000
    Inline C# Function: Get CPU usage for process, wait until execution finishes
    Begin Decimal Compare : [~~appCPU] Has Not Been Set
    End Condition - Exit when condition met
End Loop

Where "Get CPU usage for process" contains:
Code: [Select]
using System;
using System.Diagnostics;
using System.Threading;

public class VAInline
{
public void main()
{
string targetProcess = VA.GetText("~~targetProcess");
if (!String.IsNullOrEmpty(targetProcess) && Process.GetProcessesByName(targetProcess).Length != 0)
{
PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time", "VoiceAttack", true);
pc.NextValue();
Thread.Sleep(1000);
decimal appCPU = (decimal)pc.NextValue();
VA.SetDecimal("~~appCPU", appCPU);
}
else
{
VA.WriteToLog("Get CPU usage for process Error: Invalid process name \"" + (targetProcess ?? "Not set") + "\"", "red");
VA.SetDecimal("~~appCPU", null);
}
}
}


The command first checks whether a window exists (you'd want to set "~~windowTitle" to the title of the popup window) and waits until it closes.

Next, the inline function samples the CPU usage of a given process (set "~~targetProcess" to the name of the process you want to monitor) for one second, and if it's over 5% it'll wait until it goes below that (taking a same every second).
5% is just an arbitrary number I picked, you can adjust this.

The percentage is not quite consistent with what task manager shows, but it should work as an indication.
As you'll probably have a multi-core CPU, the percentage may also go over 100%. You could divide by the number of cores, but as you're only using it as an indication of activity that's not really necessary.


You'd have to insert this at the top of any command you may execute while the application is busy.
Otherwise you'd have to modify it to run in the background so it monitors continuously, and provides a way for commands to check the idle state before they execute (you could uses queues with this as well).


None of this is guaranteed to work. Windows doesn't facilitate what you're asking, as GUI applications are generally designed to interact with a human user, so the idea is that you don't provide input when you see the application is busy.

Botattack

  • Newbie
  • *
  • Posts: 3
Re: Execute only when IDLE
« Reply #4 on: July 22, 2019, 05:26:51 AM »
Thanks for the analytical reply.I have no idea hoe to use this commands, but I know what you are trying to do.
The point is that during the update there is no extra process in the task manager or a "new window"

The "updating window" is created within the app, so what is really happening is that the cpu process increases and nothing else is happening.

So I guess I may have to live with it!!!!!??