Sign in to follow this  
Followers 0
Nahuel

Timer Help

12 posts in this topic

I downloaded Autoit two days ago and I fricking love it! It's amazing! *applauds creators*

M'kay, the thing is that after making Autoit do stupid things with my computer just to have fun :rolleyes: I decided to put some effort and do something better.

I was trying to make a sctript to shut down the computer. This is what I've done so far.

#include <GuiConstants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1) 
TrayCreateItem("")
$aboutitem    = TrayCreateItem("About")
TrayCreateItem("")
$exititem      = TrayCreateItem("Stop")

GuiCreate("Shut Down PC", 165, 199)


GuiCtrlCreateLabel("Enter Time:", 2, 2)
$color=GuiCtrlCreateLabel("To stop the script click"& @CRLF & "'STOP' at the tray icon.", 2, 155)
GUICtrlSetColor($color,0xff0000)

; HORAS
GuiCtrlCreateLabel("Hours", 2, 25)
$input = GUICtrlCreateInput ("0",52,25, 50, 20)
GUICtrlCreateUpdown($input)
GUICtrlSetLimit(-1, 24,0)

;MINUTOS
GuiCtrlCreateLabel("Minutes", 2, 50)
$input2 = GUICtrlCreateInput ("0",52,50, 50, 20)
GUICtrlCreateUpdown($input2)
GUICtrlSetLimit(-1, 60,0)

;SEGUNDOS
GuiCtrlCreateLabel("Seconds", 2, 75)
$input3 = GUICtrlCreateInput ("0",52,75, 50, 20)
GUICtrlCreateUpdown($input3)
GUICtrlSetLimit(-1, 60,0)

;botón
$button1=GUICtrlCreateButton("Apply",5,120,75)
$button2=GUICtrlCreateButton("Cancel",80,120,75)

;Muestra la ventana hasta que se cierra:
GuiSetState()
TraySetState()

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $button1
        ExitLoop
    Case $msg = $button2
        Exit
    EndSelect
    $msg1 = TrayGetMsg()
    Select
        Case $msg1 = 0
            ContinueLoop
        Case $msg1 = $aboutitem
            Msgbox(0,"About","Shut Down PC" & @CRLF & "© 2007")
        Case $msg1 = $exititem
            Exit
    EndSelect
WEnd
;variables
$horas=GUICtrlRead($input)
$minutos=GUICtrlRead($input2)
$segundos=GUICtrlRead($input3)
MsgBox(0,"","Will shut down in "& $horas & " hours, "& $minutos &" minutes and " & $segundos & " seconds.")
;tiempo de apagado:
$tiempo = ($horas*3600000 + $minutos*60000 + $segundos*1000)
;dormir por el tiempo ingresado:
Sleep($tiempo)
    
Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(49,"","Will shut down in 5 seconds...",5)
Select
   Case $iMsgBoxAnswer = 1 ;OK
    MsgBox(0,"","Shutting down...",3)
   Case $iMsgBoxAnswer = 2 ;Cancel
    Exit
   Case $iMsgBoxAnswer = -1;Timeout
    MsgBox(0,"","Shutting down...",3)
EndSelect
;~ Shutdown(1)

It works the way I want if I don't want it to stop. (Except that I can't hide the main window.)

I want to make a timer (instead of using Sleep()) because I can't stop it once it started sleeping!

I want to be able to hide the main window once you press "apply" and be able to stop the timer from the tray Icon.

hope I'm being clear enough..

I'm still very new at this and I can't figure out how to use the TimerInit function :">

Thanks in advance!

Share this post


Link to post
Share on other sites



From the help file under TimerDiff ... here it comes this example:

$begin = TimerInit()
sleep(3000)
$dif = TimerDiff($begin)
MsgBox(0,"Time Difference",$dif)

All you have to do is to transform your time (hours+min+sec) in miliseconds and once TimerDiff is greater than the value to trigger the shutdown.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Thanks for the reply, but I don't know how to do that..

I already transformed my time to milliseconds here:

$tiempo = ($horas*3600000 + $minutos*60000 + $segundos*1000)

but If I do it like this:

$begin = TimerInit()

sleep($tiempo)

$dif = TimerDiff($begin)

Shutdown(1)

it won't work.

Share this post


Link to post
Share on other sites

Thank you. I'll take a look at it.

Share this post


Link to post
Share on other sites

In order to work - your code should be:

$begin = TimerInit()

While 1
Sleep (1000)
If TimerDiff($begin) > $tiempo Then Shutdown(1)
WEnd

This code will check the time once in a second (like 0% CPU usage ) and if the timer difference is bigger than $tiempo then will trigger the shutdown.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Thanks a lot for the answer guys! Really appreciated.

Now, I did what enaiman said (end of script) and it does exactly what my original script used to do. It doesn't let me stop the timer from the 'stop' option in the tray icon. I don't know what I did wrong:

#include <GuiConstants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1) 
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Stop")

GuiCreate("Shut Down PC", 165, 199)


GuiCtrlCreateLabel("Enter Time:", 2, 2)
$color=GuiCtrlCreateLabel("To stop the script click"& @CRLF & "'STOP' at the tray icon.", 2, 155)
GUICtrlSetColor($color,0xff0000)
; HORAS
GuiCtrlCreateLabel("Hours", 2, 25)
$input = GUICtrlCreateInput ("0",52,25, 50, 20)
GUICtrlCreateUpdown($input)
GUICtrlSetLimit(-1, 24,0)

;MINUTOS
GuiCtrlCreateLabel("Minutes", 2, 50)
$input2 = GUICtrlCreateInput ("0",52,50, 50, 20)
GUICtrlCreateUpdown($input2)
GUICtrlSetLimit(-1, 60,0)

;SEGUNDOS
GuiCtrlCreateLabel("Seconds", 2, 75)
$input3 = GUICtrlCreateInput ("0",52,75, 50, 20)
GUICtrlCreateUpdown($input3)
GUICtrlSetLimit(-1, 60,0)

;botón
$button1=GUICtrlCreateButton("Apply",5,120,75)
$button2=GUICtrlCreateButton("Cancel",80,120,75)

;Muestra la ventana hasta que se cierra:
GuiSetState()
TraySetState()


While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $button1
        ExitLoop
    Case $msg = $button2
        Exit
    EndSelect
    $msg1 = TrayGetMsg()
    Select
        Case $msg1 = 0
            ContinueLoop
        Case $msg1 = $aboutitem
            Msgbox(0,"About","Shut Down PC" & @CRLF & "© 2007")
        Case $msg1 = $exititem
            Exit
    EndSelect
WEnd
$horas=GUICtrlRead($input)
$minutos=GUICtrlRead($input2)
$segundos=GUICtrlRead($input3)

$tiempo = ($horas*3600000 + $minutos*60000 + $segundos*1000)

MsgBox(0,"","Will shut down in "& $horas & " hours, "& $minutos &" minutes and " & $segundos & " seconds.")

$begin = TimerInit()
While 1
Sleep (1000)
If TimerDiff($begin) > $tiempo Then 
    Dim $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(49,"","Will shut down in 5 seconds...",5)
    Select
        Case $iMsgBoxAnswer = 1  ;OK
        MsgBox(0,"","Shutting down...",3)
        ExitLoop;Shutdown(1)
        Case $iMsgBoxAnswer = 2  ;Cancel
        Exit
        Case $iMsgBoxAnswer = -1 ;Timeout
        MsgBox(0,"","Shutting down...",3)
        ExitLoop;Shutdown(1)
    EndSelect
EndIf
WEnd

Share this post


Link to post
Share on other sites

Here is a working version - I've changed the code for OnEventMode (heh - I'm a big fan of this mode :rolleyes: )

#include <GuiConstants.au3>
#NoTrayIcon

Opt("GUIOnEventMode",1)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1) 

Global $begin = 0, $tiempo

TrayCreateItem("")
$aboutitem    = TrayCreateItem("About")
TrayItemSetOnEvent(-1, "about")
TrayCreateItem("")
$exititem      = TrayCreateItem("Stop")
TrayItemSetOnEvent(-1, "close_app")

GuiCreate("Shut Down PC", 165, 199)
GUISetOnEvent($GUI_EVENT_CLOSE, "close_app")

GuiCtrlCreateLabel("Enter Time:", 2, 2)
$color=GuiCtrlCreateLabel("To stop the script click"& @CRLF & "'STOP' at the tray icon.", 2, 155)
GUICtrlSetColor($color,0xff0000)
; HORAS
GuiCtrlCreateLabel("Hours", 2, 25)
$input = GUICtrlCreateInput ("0",52,25, 50, 20)
GUICtrlCreateUpdown($input)
GUICtrlSetLimit(-1, 24,0)

;MINUTOS
GuiCtrlCreateLabel("Minutes", 2, 50)
$input2 = GUICtrlCreateInput ("0",52,50, 50, 20)
GUICtrlCreateUpdown($input2)
GUICtrlSetLimit(-1, 60,0)

;SEGUNDOS
GuiCtrlCreateLabel("Seconds", 2, 75)
$input3 = GUICtrlCreateInput ("0",52,75, 50, 20)
GUICtrlCreateUpdown($input3)
GUICtrlSetLimit(-1, 60,0)

;botón
$button1=GUICtrlCreateButton("Apply",5,120,75)
GUICtrlSetOnEvent(-1, "set_time")
$button2=GUICtrlCreateButton("Cancel",80,120,75)
GUICtrlSetOnEvent(-1, "close_app")

;Muestra la ventana hasta que se cierra:
GuiSetState()
TraySetState()


While 1
    Sleep (500)
    If $begin <> 0 Then
        If TimerDiff($begin) - 5000 > $tiempo Then 
            Dim $iMsgBoxAnswer
            $iMsgBoxAnswer = MsgBox(49,"","Will shut down in 5 seconds...",5)
            Select
                Case $iMsgBoxAnswer = 1 ;OK
                MsgBox(0,"","Shutting down...",3)
                ExitLoop;Shutdown(1)
                Case $iMsgBoxAnswer = 2 ;Cancel
                Exit
                Case $iMsgBoxAnswer = -1;Timeout
                MsgBox(0,"","Shutting down...",3)
                ExitLoop;Shutdown(1)
            EndSelect
        EndIf
    EndIf
WEnd

Func set_time()
    $begin = TimerInit()
    $horas=GUICtrlRead($input)
    $minutos=GUICtrlRead($input2)
    $segundos=GUICtrlRead($input3)
    $tiempo = ($horas*3600000 + $minutos*60000 + $segundos*1000)
    MsgBox(0,"","Will shut down in "& $horas & " hours, "& $minutos &" minutes and " & $segundos & " seconds.")
                GuiSetState(@SW_MINIMIZE)
EndFunc

Func about()
    Msgbox(0,"About","Shut Down PC" & @CRLF & "© 2007")
EndFunc


Func close_app()
    Exit
EndFunc

This is doing everything you wanted to. (added GuiSetState(@SW_MINIMIZE) - to minimize the GUI once you click Apply)

@googemyster

If you use

While 1
    Sleep(0x7FFFFFFF);A very long sleep time (No (relative) cpu usage)
WEnd

what will happen when the shutdown is intended to occur in 2 minutes and you put the script to sleep for ... 0x7FFFFFFF ?? (too lazy now to do the conversion hex-dec)

and about:

btw, wouldn't that code try to shut the computer down every second

the answer is NO - it will check the timer every second and IF the timer is bigger than set value it will trigger the shutdown.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

YES!! Thanks a lot for taking the time :rolleyes: Highly appreciated. I'll analyze the code so I can learn clearly how it works.

The only thing is that it doesn't hide the window, it minimizes it to the task bar. But that's just a minor inconvenience. Thanks again! This place is awesome.

Share this post


Link to post
Share on other sites

You're welcome :rolleyes:

To hide it change

GuiSetState(@SW_MINIMIZE)

with

GuiSetState(@SW_HIDE)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

You're da man dude! It works exactly how I wanted it to work! :rolleyes:

Thank you!

Share this post


Link to post
Share on other sites

The idea for using this code was to allow the user to stop the process (from the taskbar) anytime and that couldn't be done when sleeping :rolleyes:


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

BTW, Glad you got it working Nahuel.

Thank you.

The idea for using this code was to allow the user to stop the process (from the taskbar) anytime and that couldn't be done when sleeping :rolleyes:

Exactly, and it works perfectly! :x Since it's my first script (even though I made it with extra help) I thought I'd share it, but apparently I'm a newbie and can't post in the sharing forum :rambo:

Thanks both for your help.

Share this post


Link to post
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
Sign in to follow this  
Followers 0