Skip to content

Commit

Permalink
Add SetExecInterval, and SystemState
Browse files Browse the repository at this point in the history
  • Loading branch information
goreliu committed Apr 21, 2016
1 parent 690fc15 commit d0c5a40
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Core/Common.ahk
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
; http://ahk8.com/archive/index.php/thread-1927.html
UnicodeDecode(text)
{
return text
while pos := RegExMatch(text, "\\u\w{4}")
{
tmp := UrlEncodeEscape(SubStr(text, pos + 2, 4))
Expand All @@ -18,3 +17,41 @@ UrlEncodeEscape(text)
NumPut(text, LE)
return StrGet(&LE, 2)
}

; https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
CPULoad()
{
static PIT, PKT, PUT
if (Pit = "")
{
return 0, DllCall("GetSystemTimes", "Int64P", PIT, "Int64P", PKT, "Int64P", PUT)
}
DllCall("GetSystemTimes", "Int64P", CIT, "Int64P", CKT, "Int64P", CUT)
IdleTime := PIT - CIT, KernelTime := PKT - CKT, UserTime := PUT - CUT
SystemTime := KernelTime + UserTime
return ((SystemTime - IdleTime) * 100) // SystemTime, PIT := CIT, PKT := CKT, PUT := CUT
}

; https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
GlobalMemoryStatusEx()
{
static MEMORYSTATUSEX, init := VarSetCapacity(MEMORYSTATUSEX, 64, 0) && NumPut(64, MEMORYSTATUSEX, "UInt")
if (DllCall("Kernel32.dll\GlobalMemoryStatusEx", "Ptr", &MEMORYSTATUSEX))
{
return { 2 : NumGet(MEMORYSTATUSEX, 8, "UInt64")
, 3 : NumGet(MEMORYSTATUSEX, 16, "UInt64")
, 4 : NumGet(MEMORYSTATUSEX, 24, "UInt64")
, 5 : NumGet(MEMORYSTATUSEX, 32, "UInt64") }
}
}

; https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
GetProcessCount()
{
proc := ""
for process in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_Process")
{
proc++
}
return proc
}
17 changes: 17 additions & 0 deletions Core/Functions.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Functions:
@("AhkTest", "运行参数或者剪切板中的 AHK 代码")
@("IncreaseVolume", "提高音量")
@("DecreaseVolume", "降低音量")
@("SystemState", "系统状态 top")

if (IsLabel("ReservedFunctions"))
{
Expand Down Expand Up @@ -398,3 +399,19 @@ return
DecreaseVolume:
SoundSet, -5
return

SystemState:
if (!SetExecInterval(1))
{
return
}

GMSEx := GlobalMemoryStatusEx()
result := "* | 状态 | 运行时间 | " Round(A_TickCount / 1000 / 3600, 3) " 小时`n"
result .= "* | 状态 | CPU 占用 | " CPULoad() "% `n"
result .= "* | 状态 | 内存占用 | " Round(100 * (GMSEx[2] - GMSEx[3]) / GMSEx[2], 2) "% `n"
result .= "* | 状态 | 进程总数 | " GetProcessCount() "`n"
result .= "* | 状态 | 内存总量 | " Round(GMSEx[2] / 1024**2, 2) "MB `n"
result .= "* | 状态 | 可用内存 | " Round(GMSEx[3] / 1024**2, 2) "MB `n"
DisplayResult(AlignText(result))
return
28 changes: 28 additions & 0 deletions RunZ.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ global g_UseResultFilter
global g_UseRealtimeExec
; 排除的命令
global g_ExcludedCommands
; 间隔运行命令的间隔时间
global g_ExecInterval
; 上次间隔运行的功能标签
global g_LastExecLabel

global g_InputArea := "Edit1"
global g_DisplayArea := "Edit3"
Expand Down Expand Up @@ -619,6 +623,7 @@ return
SearchCommand(command = "", firstRun = false)
{
g_UseDisplay := false
g_ExecInterval := -1
result := ""
; 供去重使用
fullResult := ""
Expand Down Expand Up @@ -852,6 +857,22 @@ TurnOnRealtimeExec()
}
}

SetExecInterval(second)
{
; g_ExecInterval 为 0 时,表示可以进入间隔运行状态
; g_ExecInterval 为 -1 时,表示状态以被打破,需要退出
if (g_ExecInterval >= 0)
{
g_ExecInterval := second * 1000
return true
}
else
{
SetTimer, %g_LastExecLabel%, Off
return false
}
}

ClearInput:
ControlSetText, %g_InputArea%, , %g_WindowName%
ControlFocus, %g_InputArea%
Expand Down Expand Up @@ -913,6 +934,7 @@ RunCommand(originCmd)

g_UseDisplay := false
g_DisableAutoExit := true
g_ExecInterval := 0

splitedOriginCmd := StrSplit(originCmd, " | ")
cmd := splitedOriginCmd[2]
Expand Down Expand Up @@ -988,6 +1010,12 @@ RunCommand(originCmd)
{
GoSub, EscFunction
}

if (g_ExecInterval > 0 && splitedOriginCmd[1] == "function")
{
SetTimer, %cmd%, %g_ExecInterval%
g_LastExecLabel := cmd
}
}

ChangeRank(cmd, show = false, inc := 1)
Expand Down

0 comments on commit d0c5a40

Please sign in to comment.