Jump to content

Can somebody fix this ?


Recommended Posts

I cant get that timer to work caz of my math issues.

Can anyone look at it and tweak what ever needs to be tweaked so it would work right ?

Big thanks in advance for a fix or help with.

If anyone want sounds ill be happy to email them upon request :mellow:.

Here is the code

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=shutdown.ico
#AutoIt3Wrapper_outfile=..\ShutDowner.exe
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
FileInstall ("Hybernate.wav",@TempDir & "\Hybernate.wav",1)
FileInstall ("LogOff.wav",@TempDir & "\LogOff.wav",1)
FileInstall ("Restart.wav",@TempDir & "\Restart.wav",1)
FileInstall ("ShutDown.wav",@TempDir & "\ShutDown.wav",1)
FileInstall ("StandBy.wav",@TempDir & "\StandBy.wav",1)
FileInstall ("ForceOn.wav",@TempDir & "\ForceOn.wav",1)
FileInstall ("ForceOff.wav",@TempDir & "\ForceOff.wav",1)
FileInstall ("ForceFrozeOff.wav",@TempDir & "\ForceFrozeOff.wav",1)
FileInstall ("ForceFrozeOn.wav",@TempDir & "\ForceFrozeOn.wav",1)
FileInstall ("CountDown.wav",@TempDir & "\CountDown.wav",1)
FileInstall ("Error.wav",@TempDir & "\Error.wav",1)
;1Hour = "3600000"
;1Minute = "60000"
$ReadLastDay= IniRead ("Settings.ini","Time","LastDays","")
$ReadLastHour = IniRead ("Settings.ini","Time","LastHours","")
$ReadLastMinute = IniRead ("Settings.ini","Time","LastMinute","")

$Form1 = GUICreate("ShutDown Timer",260,240)
GUICtrlCreateGroup("ShutDown in:",5,3,150,180)

GUICtrlCreateLabel ("Day's",30,23)
$Days = GUICtrlCreateInput ($ReadLastDay,10,20,20,20)

GUICtrlCreateLabel ("Hr's",80,23)
$Hours = GUICtrlCreateInput ($ReadLastHour,60,20,20,20)
GUICtrlSetLimit ($Hours,2)

GUICtrlCreateLabel ("Min's",125,23,25)
$Minutes = GUICtrlCreateInput ($ReadLastMinute,105,20,20,20)
GUICtrlSetLimit ($Minutes,2)

$optionInput = GUICtrlCreateInput ("Please select desired action",10,40,140)
$reasonInput = GUICtrlCreateEdit ("No reason specified",10,60,140,115)

GUICtrlCreateGroup ("Action's:",160,3,90,120)
$optionLogoff = GUICtrlCreateRadio ("LogOff",170,20)
$optionShutDown = GUICtrlCreateRadio ("ShutDown",170,40)
$optionReboot = GUICtrlCreateRadio ("Reboot",170,60)
$optionStandby = GUICtrlCreateRadio ("Standby",170,80)
$optionHibernate = GUICtrlCreateRadio ("Hibernate",170,100)

GUICtrlCreateGroup ("Optional:",160,123,90,60)
$optionForceCloseApps = GUICtrlCreateCheckbox ("Force",165,140)
$optionForceAll = GUICtrlCreateCheckbox ("Force if hung",165,160)

$button = GUICtrlCreateButton ("Start Count Down",10,200,240,30)

Global $ActionRead

GuiFunc()
Func GuiFunc()
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
Exit

Case $button
    GUISetState (@SW_HIDE)
$ActionRead = GUICtrlRead ($optionInput)
$ReadDays = GUICtrlRead ($Days)
$ReadHours = GUICtrlRead ($Hours)
$ReadMinutes = GUICtrlRead ($Minutes)

If $ReadDays >= 365 Then
    MsgBox (64,"Days Error", "Please set from 0-59 minutes only")
EndIf

If $ReadHours >= 23 Then
    MsgBox (64,"Hours Error", "Please set from 0-59 minutes only")
EndIf
If $ReadMinutes >= 60 Then
    MsgBox (64,"Minutes Error", "Please set from 0-23 hours only")
EndIf

If $ActionRead = "What to do" Then
    MsgBox(16,'Error','Please select action first')
    SoundPlay (@TempDir & "\Error.wav",0)
    GuiFunc()
EndIf
SoundPlay (@TempDir & "\CountDown.wav",0)
$DaysRead = GUICtrlRead ($Days)
$HoursRead = GUICtrlRead ($Hours)
$MinutesRead = GUICtrlRead ($Minutes)
IniWrite ("Settings.ini","Time","LastDays",$DaysRead)
IniWrite ("Settings.ini","Time","LastHours",$HoursRead)
IniWrite ("Settings.ini","Time","LastMinute",$MinutesRead)

;$TotalTime = ($HourMultiplied + $MinuteMultiplied)

$option = GUICtrlRead ($optionInput)
$reason = GUICtrlRead ($reasonInput)
$ForceCloseRead = GUICtrlRead ($optionForceCloseApps)
$ForceFrozeRead = GUICtrlRead ($optionForceAll)
GUICtrlSetData ($button,"Cancel")

_SleepAndDisplayDays()
_SleepAndDisplayHours() ;this function will run before shut down. Only when function is finished, shut down will execute
_SleepAndDisplayMinutes()
MsgBox(0,'',$option)
$optionConverted = ""
If $option = "LogOff" Then
    Assign ("optionConverted","0")
ElseIf $option = "Shutdown" Then
    Assign ("optionConverted","1")
ElseIf $option = "Reboot" Then
    Assign ("optionConverted","2")
ElseIf $option = "Standby" Then
    Assign ("optionConverted","32")
ElseIf $option = "Hibernate" Then
    Assign ("optionConverted","1")
EndIf

#Region ;ShutDown goes here
Shutdown ($optionConverted,$reason)
exit
#Region

$Finaloption = ""
If $ForceCloseRead = "1" And $ForceFrozeRead = "1" Then
    Assign ("option",$option & "+4+16")
    Shutdown ($Finaloption,$reason)
ElseIf $ForceCloseRead = "1" And $ForceFrozeRead = "4" Then
    Assign ("option",$option & "+4")
    Shutdown ($Finaloption,$reason)
ElseIf $ForceCloseRead = "4" And $ForceFrozeRead = "1" Then
    Assign ("option",$option & "+16")
    Shutdown ($Finaloption,$reason)
ElseIf $ForceCloseRead = "4" And $ForceFrozeRead = "4" Then
    Assign ("option",$option & "+16")
    Shutdown ($Finaloption,$reason)
Else
    MsgBox(64,"","Unknown error at line 70. Unable to determine optional components.")
    Exit
Endif

Case $optionLogoff
    GUICtrlSetData ($optionInput,"Logoff")
    SoundPlay (@TempDir & "\LogOff.wav",0)
    Assign ("option","0")
Case $optionShutDown
    GUICtrlSetData ($optionInput,"ShutDown")
    SoundPlay (@TempDir & "\ShutDown.wav",0)
    Assign ("option","1")
Case $optionReboot
    GUICtrlSetData ($optionInput,"Reboot")
    SoundPlay (@TempDir & "\Restart.wav",0)
    Assign ("option","2")
Case $optionStandby
    GUICtrlSetData ($optionInput,"Standby")
    SoundPlay (@TempDir & "\StandBy.wav",0)
    Assign ("option","32")
Case $optionHibernate
    GUICtrlSetData ($optionInput,"Hibernate")
    SoundPlay (@TempDir & "\Hybernate.wav",0)
    Assign ("option","64")
Case $optionForceCloseApps
    If GUICtrlRead ($optionForceCloseApps) = $GUI_CHECKED Then
    SoundPlay (@TempDir & "\ForceOn.wav",0)
    Elseif GUICtrlRead ($optionForceCloseApps) = $GUI_UNCHECKED Then
    SoundPlay (@TempDir & "\ForceOff.wav",0)
    Endif
Case $optionForceAll
    If GUICtrlRead ($optionForceAll) = $GUI_CHECKED Then
    SoundPlay (@TempDir & "\ForceFrozeOn.wav",0)
    Elseif GUICtrlRead ($optionForceAll) = $GUI_UNCHECKED Then
    SoundPlay (@TempDir & "\ForceFrozeOff.wav",0)
    Endif
EndSwitch
WEnd
EndFunc

Func _SleepAndDisplayDays()
$DaysMultiplied = ($Days)
; $sleepTime determines how long to sleep, $checkEveryMS determines how often should be checked (in example: 100ms checks 10 times per second (1000/100 = 10))
$TimeCollected = ""
while 1
    if $TimeCollected = $DaysMultiplied Then ; If the time for sleeping has passed total time which is devided by  60000 to be equal to seconds, we can go out of the loop
ExitLoop
EndIf

Sleep("1000") ; Wait a while before we do the next step. If we omit this step then CPU message will skyrocket.
$AddTime = ($TimeCollected + 1)
Assign ("TimeCollected",$AddTime)
ToolTip ($TimeCollected & " Out of " & $DaysMultiplied ,0,0,$ActionRead & " in")
WEnd

Return
EndFunc


Func _SleepAndDisplayHours()
$HourMultiplied = ($Hours)
; $sleepTime determines how long to sleep, $checkEveryMS determines how often should be checked (in example: 100ms checks 10 times per second (1000/100 = 10))
$TimeCollected = ""
while 1
    if $TimeCollected = $HourMultiplied Then ; If the time for sleeping has passed total time which is devided by  60000 to be equal to seconds, we can go out of the loop
ExitLoop
EndIf

Sleep("1000") ; Wait a while before we do the next step. If we omit this step then CPU message will skyrocket.
$AddTime = ($TimeCollected + 1)
Assign ("TimeCollected",$AddTime)
ToolTip ($TimeCollected & " Out of " & $HourMultiplied ,0,0,$ActionRead & " in")
WEnd

Return
EndFunc

Func _SleepAndDisplayMinutes()
$MinuteMultiplied = ($Minutes)
; $sleepTime determines how long to sleep, $checkEveryMS determines how often should be checked (in example: 100ms checks 10 times per second (1000/100 = 10))
$TimeCollected = ""
while 1
    if $TimeCollected = $MinuteMultiplied Then ; If the time for sleeping has passed total time which is devided by  60000 to be equal to seconds, we can go out of the loop
ExitLoop
EndIf

Sleep("1000") ; Wait a while before we do the next step. If we omit this step then CPU message will skyrocket.
$AddTime = ($TimeCollected + 1)
Assign ("TimeCollected",$AddTime)
ToolTip ($TimeCollected & " Out of " & $MinuteMultiplied ,0,0,$ActionRead & " in")
WEnd

Return
EndFunc
Link to comment
Share on other sites

You should start doing correctly simpler scripts before.

Sleep("1000") is incorrect, use Sleep(1000)

Why do you keep using Assign?

Assign ("TimeCollected",$AddTime) is the same as$TimeCollected = $AddTime

You don't need extra parenthesis$AddTime = $TimeCollected + 1

Anything after Exit won't execute:

#Region ;ShutDown goes here

Shutdown ($optionConverted,$reason)

exit

#Region

<<the rest won't execute>>

Trim down the useless stuff (gui) and expose your working logic as it's "a bit" hard to follow.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Also you're calling GUIfunc from itsself.

I'll see if I can tidy it up a bit so you can play "spot the difference"

edit:

I modified your code to something that should do what you wanted. There is still room for allot of improvement, but atleast it works and it should show you some alternatives to the methods you used.

If you have any questions about my code just post em here and me, or someone else will probably answer them.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>

Global $option

FileInstall ("Hybernate.wav",@TempDir & "\Hybernate.wav",1)
FileInstall ("LogOff.wav",@TempDir & "\LogOff.wav",1)
FileInstall ("Restart.wav",@TempDir & "\Restart.wav",1)
FileInstall ("ShutDown.wav",@TempDir & "\ShutDown.wav",1)
FileInstall ("StandBy.wav",@TempDir & "\StandBy.wav",1)
FileInstall ("ForceOn.wav",@TempDir & "\ForceOn.wav",1)
FileInstall ("ForceOff.wav",@TempDir & "\ForceOff.wav",1)
FileInstall ("ForceFrozeOff.wav",@TempDir & "\ForceFrozeOff.wav",1)
FileInstall ("ForceFrozeOn.wav",@TempDir & "\ForceFrozeOn.wav",1)
FileInstall ("CountDown.wav",@TempDir & "\CountDown.wav",1)
FileInstall ("Error.wav",@TempDir & "\Error.wav",1)

$ReadLastDay = IniRead("Settings.ini", "Time", "LastDays", "")
$ReadLastHour = IniRead("Settings.ini", "Time", "LastHours", "")
$ReadLastMinute = IniRead("Settings.ini", "Time", "LastMinute", "")

$Form1 = GUICreate("ShutDown Timer", 260, 240)
GUICtrlCreateGroup("ShutDown in:", 5, 3, 150, 180)

GUICtrlCreateLabel("Day's", 30, 23)
$Days = GUICtrlCreateInput($ReadLastDay, 10, 20, 20, 20)

GUICtrlCreateLabel("Hr's", 80, 23)
$Hours = GUICtrlCreateInput($ReadLastHour, 60, 20, 20, 20)
GUICtrlSetLimit($Hours, 2)

GUICtrlCreateLabel("Min's", 125, 23, 25)
$Minutes = GUICtrlCreateInput($ReadLastMinute, 105, 20, 20, 20)
GUICtrlSetLimit($Minutes, 2)

$optionInput = GUICtrlCreateInput("Please select desired action", 10, 40, 140)
$reasonInput = GUICtrlCreateEdit("No reason specified", 10, 60, 140, 115)

GUICtrlCreateGroup("Action's:", 160, 3, 90, 120)
$optionLogoff = GUICtrlCreateRadio("LogOff", 170, 20)
$optionShutDown = GUICtrlCreateRadio("ShutDown", 170, 40)
$optionReboot = GUICtrlCreateRadio("Reboot", 170, 60)
$optionStandby = GUICtrlCreateRadio("Standby", 170, 80)
$optionHibernate = GUICtrlCreateRadio("Hibernate", 170, 100)

GUICtrlCreateGroup("Optional:", 160, 123, 90, 60)
$optionForceCloseApps = GUICtrlCreateCheckbox("Force", 165, 140)
$optionForceAll = GUICtrlCreateCheckbox("Force if hung", 165, 160)

$button = GUICtrlCreateButton("Start Count Down", 10, 200, 240, 30)
GUISetState(@SW_SHOW)

GuiFunc()
Func GuiFunc()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $button
                GUISetState(@SW_HIDE)
                _StartCountDown()
            Case $optionLogoff
                GUICtrlSetData($optionInput, "Logoff")
                $option = 0
            Case $optionShutDown
                GUICtrlSetData($optionInput, "ShutDown")
                $option = 1
            Case $optionReboot
                GUICtrlSetData($optionInput, "Reboot")
                $option = 2
            Case $optionStandby
                GUICtrlSetData($optionInput, "Standby")
                $option = 32
            Case $optionHibernate
                GUICtrlSetData($optionInput, "Hibernate")
                $option = 64
            Case $optionForceCloseApps
                If GUICtrlRead($optionForceCloseApps) = $GUI_CHECKED Then
                    SoundPlay(@TempDir & "\ForceOn.wav", 0)
                ElseIf GUICtrlRead($optionForceCloseApps) = $GUI_UNCHECKED Then
                    SoundPlay(@TempDir & "\ForceOff.wav", 0)
                EndIf
            Case $optionForceAll
                If GUICtrlRead($optionForceAll) = $GUI_CHECKED Then
                    SoundPlay(@TempDir & "\ForceFrozeOn.wav", 0)
                ElseIf GUICtrlRead($optionForceAll) = $GUI_UNCHECKED Then
                    SoundPlay(@TempDir & "\ForceFrozeOff.wav", 0)
                EndIf
        EndSwitch
    WEnd
EndFunc ;==>GuiFunc

Func _StartCountDown()
    Local $ActionRead = GUICtrlRead($optionInput)
    Local $ReadDays = GUICtrlRead($Days)
    Local $ReadHours = GUICtrlRead($Hours)
    Local $ReadMinutes = GUICtrlRead($Minutes)
    Local $ForceCloseRead = GUICtrlRead($optionForceCloseApps)
    Local $ForceFrozeRead = GUICtrlRead($optionForceAll)
    Local $reason = GUICtrlRead($reasonInput)

    If $ReadDays >= 365 Then
        MsgBox(64, "Days Error", "Please set from 0-364 days only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ReadHours >= 23 Then
        MsgBox(64, "Hours Error", "Please set from 0-23 hours only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ReadMinutes >= 60 Then
        MsgBox(64, "Minutes Error", "Please set from 0-59 minutes only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ActionRead = "What to do" Then
        MsgBox(16, 'Error', 'Please select action first')
        SoundPlay(@TempDir & "\Error.wav", 0)
        GUISetState(@SW_SHOW)
        Return
    EndIf
    SoundPlay(@TempDir & "\CountDown.wav", 0)
    IniWrite("Settings.ini", "Time", "LastDays", $ReadDays)
    IniWrite("Settings.ini", "Time", "LastHours", $ReadHours)
    IniWrite("Settings.ini", "Time", "LastMinute", $ReadMinutes)
    If $ForceCloseRead = 1 Then $option += 4
    If $ForceFrozeRead = 1 Then $option += 16

    $timer = TimerInit()
    $ShutDowntime = (((($ReadDays*24)+$ReadHours)*60)+$ReadMinutes)*60*1000
    While 1
        Local $ms = $ShutDowntime - TimerDiff($timer)
        If $ms < 0 Then
            MsgBox(0,"test","Shutdown")
            Shutdown($option,$reason)
            Exit
        Else
            ToolTip("Shutdown in " & StringFormat("%d days and \n%02d:%02d:%02d", Int($ms / 86400000), Mod(Int($ms / 3600000),24), Mod(Int($ms / 60000),60), Mod(Int($ms / 1000),60)),0,0)
        EndIf
        Sleep(100)
    WEnd
EndFunc
Edited by Tvern
Link to comment
Share on other sites

Tvern

Can you explane me how this work:

$timer = TimerInit()
    $ShutDowntime = (((($ReadDays*24)+$ReadHours)*60)+$ReadMinutes)*60*1000
    While 1
        Local $ms = $ShutDowntime - TimerDiff($timer)
        If $ms < 0 Then
            MsgBox(0,"test","Shutdown")
            Shutdown($option,$reason)
            Exit
        Else
            ToolTip("Shutdown in " & StringFormat("%d days and \n%02d:%02d:%02d", Int($ms / 86400000), Mod(Int($ms / 3600000),24), Mod(Int($ms / 60000),60), Mod(Int($ms / 1000),60)),0,0)
        EndIf
        Sleep(100)
    WEnd

Do the best you can (if you want).

I just want to understand how did you calculate total time and then display it counting down correctly.

Caz what i had in mind was to calculate based on initial time and calculate whole ammount of microseconds from given total time which is multiplied by days in a year, hours in a day and seconds in an hour

Then i would somehow (that was the plan) count it back.

But yours seems so much easier and in just a few lines.

As for (code could use more improvements) i dont care as long as it does the job.

Edited by madasraka
Link to comment
Share on other sites

I've commented it the best I could. I hope it's clear.

For more in-depth explanations of what I did look in the helpfile for:

TimerInit() + TimerDiff()

Int()

Mod()

and especially

StringFormat()

$timer = TimerInit() ;this gives a timestamp of the current time, to be used with timerdiff
$ShutDowntime = (((($ReadDays*24)+$ReadHours)*60)+$ReadMinutes)*60*1000 ;this converts the total time to wait to miliseconds
While 1 ;a loop!
    Local $ms = $ShutDowntime - TimerDiff($timer) ;this substracts the amount of miliseconds passed since TimerInit() from the total time to wait
    ;$ms is now the remaining amount of miliseconds to wait, but we want to display it as days, hours, minutes and seconds.
    If $ms < 0 Then ;if $ms < 0 the time is up and Shutdown() is executed.
        MsgBox(0,"test","Shutdown")
        Shutdown($option,$reason) 
        Exit
    Else ;if $ms > 0 there is still some time left and we want to display it as a tooltip.
        ;ToolTip("Shutdown in " & StringFormat("%d days and \n%02d:%02d:%02d", Int($ms / 86400000), Mod(Int($ms / 3600000),24), Mod(Int($ms / 60000),60), Mod(Int($ms / 1000),60)),0,0)
        ;I'll break down the tooltip:
        
        $days = Int($ms / 86400000) ;the Integer of the amount of miliseconds left, devided by the amount of miliseconds in a day is the amount of days left
        $hours = Int($ms / 3600000) ;the Integer of the amount of miliseconds left, devided by the amount of miliseconds in an hour is the total amount of hours left
        $hours = Mod($hours,24) ;Basically takes of 24 hours, untill $hours < 24, meaning it won't display the amount of days left as hours. (please read the helpfile on Mod()
        $minutes = Int($ms / 60000) ;the Integer of the amount of miliseconds left, devided by the amount of miliseconds in a minute is the total amount of minutes left
        $minutes = Mod($minutes,60) ;Basically takes of 60 minutes, untill $minutes < 60, meaning it won't display the amount of hours left as minutes.
        $seconds = Mod(Int($ms / 1000),60) ;same as above, but on 1 line
        
        $format_control = "%d" ;is the value of $days displayed as an Integer
        $format_control &= " days and" ;adds the literal string " days and"
        $format_control &= "\n" ;adds a newline
        $format_control &= "%02d" ;adds the value of $hours displayed as an Integer padded with a leading "0" if needed
        $format_control &= ":" ;adds the literal string ";"
        $format_control &= "%02d" ;adds the value of $minutes displayed as an Integer padded with a leading "0" if needed
        $format_control &= ":" ;adds the literal string ";"
        $format_control &= "%02d" ;adds the value of $seconds displayed as an Integer padded with a leading "0" if needed

        $string = StringFormat($format_control, $days, $hours, $minutes, $seconds) ;format the amount of time we calculated earlier in the way dictated by the format control.
        
        ToolTip($string,0,0) ;display the result of Stringformat
    EndIf
    Sleep(100) ;reduce CPU
WEnd
Edited by Tvern
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...