- Offizieller Beitrag
Ich habe mir gerade eine Funktion zum ermitteln der Prozessorauslastung geschrieben.
Das klappt auch, aber wenn ich die Funktion innerhalb einer Timer-Funktion aufrufe und diese Timer-Funktion bei Scriptende "kille", dann stürzt AutoIt ab.
Aber nicht immer, sondern nur im 64-Bit-Modus #AutoIt3Wrapper_UseX64=y.
Im 32-Bit-Modus passiert das nicht #AutoIt3Wrapper_UseX64=n.
Könnt ihr einen Grund dafür erkennen?
BTW: Wenn ich bei Scriptende nicht aufräume _Timer_KillAllTimers($hMainGui) auskommentiere, dann beendet sich AutoIt ohne Absturz.
C
#AutoIt3Wrapper_UseX64=y ; 64Bit-Modus
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <Timers.au3>
#include <WinAPISys.au3>
Opt('GuiOnEventMode', 1)
Global $hMainGui = GUICreate('CPU-Test', 300, 420)
GUISetOnEvent($GUI_EVENT_CLOSE, '_CloseMainGui')
Global $idCPUCycle = GUICtrlCreateProgress(20, 10, 12, 400, $PBS_VERTICAL)
Global $idProgressLabel = GUICtrlCreateLabel('', 60, 30, 80, 20)
GUICtrlSetFont(-1, 16)
GUISetState()
Global $idTimer = _Timer_SetTimer($hMainGui, 250, '_CPUCycleTime')
While True
;~ Sleep(1000)
WEnd
Func _CPUCycleTime($hWnd, $iMsg, $iIDTimer, $iTime)
#forceref $hWnd, $iMsg, $iIDTimer, $iTime
Local $iPercent = _GetCPUUsage()
GUICtrlSetData($idCPUCycle, $iPercent)
GUICtrlSetData($idProgressLabel, $iPercent & '%')
EndFunc
Func _GetCPUUsage()
Local Static $aOld[3], $iTicksOld, $aSystemInfo
Local $aRet, $iTime1, $iTime2, $iPercent, $iTicks
$iTicks = _WinAPI_GetTickCount()
$aRet = _WinAPI_GetSystemTimes()
If @error Then Return SetError(@error, @extended, 0)
If $aOld[0] = 0 Then ; beim ersten Aufruf die Static-Variablen fuellen
$aSystemInfo = _WinAPI_GetSystemInfo() ; $aSystemInfo[5] = Number of physical processors
If @error Then Return SetError(@error, @extended, 0)
$iTicksOld = $iTicks
$aOld = $aRet
Return
EndIf
$iTime1 = $aRet[1] - $aRet[0] + $aRet[2] ; [0] = idle-time, [1] = kernel-time, [2] = user-time
$iTime2 = $aOld[1] - $aOld[0] + $aOld[2] ; [0] = idle-time, [1] = kernel-time, [2] = user-time
$iPercent = Int(($iTime1 - $iTime2) / (($iTicks - $iTicksOld) * 100) / $aSystemInfo[5])
$iTicksOld = $iTicks
$aOld = $aRet
Return $iPercent
EndFunc
Func _CloseMainGui()
_Timer_KillAllTimers($hMainGui)
;~ _Timer_KillTimer($hMainGui, $idTimer)
Exit
EndFunc
Alles anzeigen