I am trying to run a program during periods of low CPU Usage - Say when usage falls below 5% for 10sec.
Some days ago I found a script which I thought I could modify to do this, but have been unable to find it since!
Can anyone please help?
I am a newbie to scripting, so please keep the reply simple!!!
CPU Usage
Moderators: JRL, Dorian (MJT support)
VBScript to calculate CPU utilization
If you have version 12 of Macro Scheduler, then you can implement the sleep(2000) inside of VBScript instead of having to use a Wait>2 which
will simplify the code somewhat.
This has only been tested on a one (1) processor machine.
But this example should give you an idea how to calculate the CPU usage of the computer.
[code]
VBStart
Function CPUUsage()
Set objWMIService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process where Name = 'Idle'")
For Each objItem In colItems
N1 = objItem.PercentProcessorTime
D1 = objItem.TimeStamp_Sys100NS
Next
CPUUsage = N1 & "," & D1
End Function
VBEnd
Let>comma=,
VBEval>CPUUsage(),N1D1
Separate>N1D1,comma,itemArray
Let>N1=%itemArray_1%
Let>D1=%itemArray_2%
Wait>2
VBEval>CPUUsage(),N2D2
Separate>N2D2,comma,itemArray
Let>N2=%itemArray_1%
Let>D2=%itemArray_2%
Let>Nd={%N2% - %N1%}
Let>Dd={%D2% - %D1%}
Let>PercentProcessorTime={( (%Nd% / %Dd%)) * 100}
VBEval>Round(%PercentProcessorTime%,0),PercentProcessorTime
Let>PercentProcessorTime=100-PercentProcessorTime
MessageModal>Processor Usage Percent = %PercentProcessorTime%
[/code]
Enjoy!
will simplify the code somewhat.
This has only been tested on a one (1) processor machine.
But this example should give you an idea how to calculate the CPU usage of the computer.
[code]
VBStart
Function CPUUsage()
Set objWMIService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process where Name = 'Idle'")
For Each objItem In colItems
N1 = objItem.PercentProcessorTime
D1 = objItem.TimeStamp_Sys100NS
Next
CPUUsage = N1 & "," & D1
End Function
VBEnd
Let>comma=,
VBEval>CPUUsage(),N1D1
Separate>N1D1,comma,itemArray
Let>N1=%itemArray_1%
Let>D1=%itemArray_2%
Wait>2
VBEval>CPUUsage(),N2D2
Separate>N2D2,comma,itemArray
Let>N2=%itemArray_1%
Let>D2=%itemArray_2%
Let>Nd={%N2% - %N1%}
Let>Dd={%D2% - %D1%}
Let>PercentProcessorTime={( (%Nd% / %Dd%)) * 100}
VBEval>Round(%PercentProcessorTime%,0),PercentProcessorTime
Let>PercentProcessorTime=100-PercentProcessorTime
MessageModal>Processor Usage Percent = %PercentProcessorTime%
[/code]
Enjoy!