Jump to content

Timer Example with Very low cpu use **Updated


Guest
 Share

Recommended Posts

Hello.

I searched in the forum for goot timer script and found that no one did the simplest code and low cpu use.

some pepole don't need that their timer will think too much every 20ms.. in some used case it is a stupid idea to use TimerDiff() in fast loop(like 20ms sleep)

if the cpu was a human so he was supposed to complain...

so what i decided to do is a new timer that do the same thing but much easier for the processor..

this is the timer:

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)


Local $hForm = GUICreate("CounterTimer Sample", 184, 66, -1, 250, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitApp")
Local $Lbl_hour = GUICtrlCreateLabel("0", 16, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 56, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_min = GUICtrlCreateLabel("0", 72, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 112, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_sec = GUICtrlCreateLabel("0", 128, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_disc = GUICtrlCreateLabel("Made by gil900", 40, 3, 100, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)

Local $h = 0 , $m = 0 , $s = 0
While 1
    Sleep(1000)
    $s = $s+1
    If $s = 60 Then
        $s = 0
        $m = $m+1
        If $m < 60 Then GUICtrlSetData($Lbl_min,$m)
    EndIf
    If $m = 60 Then
        $m = 0
        $h = $h+1
        GUICtrlSetData($Lbl_min,$m)
        GUICtrlSetData($Lbl_hour,$h)
    EndIf
    GUICtrlSetData($Lbl_sec,$s)
WEnd


Func _ExitApp()
    GUIDelete()
    Exit
EndFunc

credit for newsak2005 in this topic:

for the GUI work.. I just replaced the motor :)

EDIT:

i make a new Engine Based on TimerDiff.

I think it's the smallest example that someone did ..

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)


Local $hForm = GUICreate("CounterTimer Sample", 184, 66, -1, 250, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitApp")
Local $Lbl_hour = GUICtrlCreateLabel("0", 16, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 56, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_min = GUICtrlCreateLabel("0", 72, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 112, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_sec = GUICtrlCreateLabel("0", 128, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_disc = GUICtrlCreateLabel("Made by gil900", 40, 3, 100, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)

Local  $begin = TimerInit() , $h = 0 , $m = 0 , $s = 0
While 1
    Sleep(20)
    If TimerDiff($begin) >= 980 Then
        $begin = TimerInit()
        $s = $s+1
        If $s = 60 Then
            $s = 0
            $m = $m+1
            If $m < 60 Then GUICtrlSetData($Lbl_min,$m)
        EndIf
        If $m = 60 Then
            $m = 0
            $h = $h+1
            GUICtrlSetData($Lbl_min,$m)
            GUICtrlSetData($Lbl_hour,$h)
        EndIf
        GUICtrlSetData($Lbl_sec,$s)
    EndIf
WEnd


Func _ExitApp()
    GUIDelete()
    Exit
EndFunc
Edited by Guest
Link to comment
Share on other sites

Good luck getting that script to respond to any controls you might use with it, such as buttons or being able to close a GUI. The long sleep is going to make it very difficult to action any controls in a script. This would only work in OnEvent mode, and even then it would be sluggish.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Good luck getting that script to respond to any controls you might use with it, such as buttons or being able to close a GUI. The long sleep is going to make it very difficult to action any controls in a script. This would only work in OnEvent mode, and even then it would be sluggish.

I know.

But for my purpose I do not need more than that.

so i made this example for pepole like me..

In any case, the GUI closes easily when you want to close it.

Feel free to try

Edited by Guest
Link to comment
Share on other sites

i make a new Engine Based on TimerDiff.

I think it's the smallest example that someone did ..

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)


Local $hForm = GUICreate("CounterTimer Sample", 184, 66, -1, 250, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitApp")
Local $Lbl_hour = GUICtrlCreateLabel("0", 16, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 56, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_min = GUICtrlCreateLabel("0", 72, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel(":", 112, 16, 13, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_sec = GUICtrlCreateLabel("0", 128, 20, 40, 41, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
Local $Lbl_disc = GUICtrlCreateLabel("Made by gil900", 40, 3, 100, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)

Local  $begin = TimerInit() , $h = 0 , $m = 0 , $s = 0
While 1
    Sleep(20)
    If TimerDiff($begin) >= 980 Then
        $begin = TimerInit()
        $s = $s+1
        If $s = 60 Then
            $s = 0
            $m = $m+1
            If $m < 60 Then GUICtrlSetData($Lbl_min,$m)
        EndIf
        If $m = 60 Then
            ;ToolTip("test")
            $m = 0
            $h = $h+1
            GUICtrlSetData($Lbl_min,$m)
            GUICtrlSetData($Lbl_hour,$h)
        EndIf
        GUICtrlSetData($Lbl_sec,$s)
    EndIf
WEnd


Func _ExitApp()
    GUIDelete()
    Exit
EndFunc
Edited by Guest
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...