Jump to content

Script starting even though conditions are not met


Recommended Posts

Any help on this would be appreciated. Trying to run a script every 2 hours and it works most of the time but after 9am it goes haywire and starts no matter what.

$STARTTIME = Inputbox("What Time Do You Want to Start", "Please fill in your Start Time H:MM:SS AM/PM" )
$STARTDATE = _NowDate()

While 1
    If _NowTime() > $STARTTIME AND _NowDate() = $STARTDATE  Then
        startWF()
    Else
        Sleep(5000)
    EndIf
WEnd

func startWF()
    $STARTTIME = _DateAdd( 'h' ,2, _NowCalc()) ;Adds 2 Hours to our Start Time
    $STARTTIME = _DateTimeFormat($STARTTIME,3) ;Formats $StartTime to PC's Time format
    $STARTTIME = StringReplace($STARTTIME, _NowCalcDate(), "")  ;removes Date from $StartTime
    $STARTDATE = _DateAdd('h' ,2, _NowCalc()) ;Adds 2 hours to Date will flip to next day at 10-11pm
    $STARTDATE = _DateTimeFormat($STARTDATE,2) ;Formats Date to just Date
    GUICtrlSetData ($TimeLbl, ($STARTTIME))

Do STUFF

EndFunc

That is the snippit that is causing problems. And not sure why. It seems to run fine for hours and then for whatever reason it messes up and starts when it shouldn't. Even restarting doesn't help it.

NOTE: I had to seperate the date and time as I couldn't get it to parse correctly for the start condition together.

Second Note: After 10am here it functions properly again. So what is happening between 9:00-9:59 AM/PM

Edited by SiliconeClone
Link to comment
Share on other sites

@SiliconeClone: In case you don't know why: Your $STARTTIME is a string and "_NowTime() > $STARTTIME" is not a valid string comparison. That's why rudi says to use _DateDiff()

:graduated:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

@SiliconeClone: In case you don't know why: Your $STARTTIME is a string and "_NowTime() > $STARTTIME" is not a valid string comparison. That's why rudi says to use _DateDiff()

:graduated:

I can't seem how to figure out how to use _datediff here. I mean I have to enter the time as a string in order tell it when to run. Also like I said it works except from between 9:00am/pm-9:59am/pm

Just that one hour gap is giving me problems.And I can't seem to get _DateDiff to compare properly to my inputted start time. :(

Seriously try it. Set your time between 9am and 10am or 9pm and 10pm real quick compile and see. Then change the time back to normal and watch it run properly. (ie it won't run til the $STARTTIME)

#include <Date.au3>

$STARTTIME = Inputbox("What Time Do You Want to Start", "Please fill in your Start Time H:MM:SS AM/PM" )
$STARTDATE = _NowDate()
MsgBox( 4096, "", "Start Time is: " & $STARTDATE)

While 1
    If _NowTime() > $STARTTIME AND _NowDate() = $STARTDATE  Then
        startWF()
    Else
        Sleep(5000)
    EndIf
WEnd


func startWF()
    $STARTTIME = _DateAdd( 's' ,10, _NowCalc()) ;Adds 2 Hours to our Start Time
    $STARTTIME = _DateTimeFormat($STARTTIME,3) ;Formats $StartTime to PC's Time format
    $STARTTIME = StringReplace($STARTTIME, _NowCalcDate(), "")  ;removes Date from $StartTime
    $STARTDATE = _DateAdd('h' ,2, _NowCalc()) ;Adds 2 hours to Date will flip to next day at 10-11pm
    $STARTDATE = _DateTimeFormat($STARTDATE,2) ;Formats Date to just Date
    MsgBox( 4096, "", "Start Time is: " & $STARTTIME & @lf & _nowdate() )
sleep(10000)
EndFunc
Link to comment
Share on other sites

Be careful of the many ways the date/time can be formatted:

#include <Date.au3>

$StartTime = InputBox("What Time Do You Want to Start", "Please fill in your Start Time H:MM:SS AM/PM")
$StartDate = _NowCalcDate() ; Date as YYYY/MM/DD
$StartTime = $StartDate & " " & $StartTime ; Date/Time as YYYY/MM/DD hh:mm:ss AM

MsgBox(64, "Result", "Start Time is: " & $StartTime)

While 1
    If (_DateDiff("s", $StartTime, _NowCalc()) > 0) AND _NowCalcDate() = $StartDate  Then
        startWF()
    Else
        Sleep(5000)
    EndIf
WEnd

:graduated:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Didn't quite work but it helped... Now my only problem is before it would add 2 hours to date (which once ran after 10PM it would change teh date to the next day) the script now runs as it shuold except once I get to where it should flip the date it is not doing so.

IE once it gets to 11am it is suppose to run again on 1am the next day.. BUT because the date doesn't change it gets stuck in the loop.

Here it is with some adjustments to make it work ( using some of your info! )

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

Opt("GUIOnEventMode", 1)
GUICreate("Auto Warfield", 250, 125)
GUISetState (@SW_SHOW)
GUISetBkColor (0xEBE9ED)
$charbutton = GUICtrlCreateButton("Change Secondary", 5, 90, 120)
$endbutton = GUICtrlCreateButton("End Warfields", 125, 90, 120)
GUICtrlCreateLabel("Start Date:", 5, 10, 150, 20)
GUICtrlCreateLabel("Starting Time:", 5, 40, 170, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "endWF")
GUICtrlSetOnEvent($endbutton, "endWF")

$StartTime = InputBox("What Time Do You Want to Start", "Please fill in your Start Time H:MM:SS AM/PM")
$StartDate = _NowCalcDate() ; Date as YYYY/MM/DD


;Start Time Label
$TimeLbl = GUICtrlCreateLabel($STARTTIME, 125, 40, 120, 20)
GUICtrlSetFont(-1, 8.5, 800)
GUICtrlSetFont(-1, 8.5, 800)

;Start Time Label
$DateLbl = GUICtrlCreateLabel($STARTDATE, 125, 10, 120, 20)
GUICtrlSetFont(-1, 8.5, 800)
GUICtrlSetFont(-1, 8.5, 800)


;MsgBox(64, "Result", "Start Time is: " & $StartTime)

While 1
    If $StartTime < _NowTime() AND _NowCalcDate() = $StartDate  Then
        startWF()
    Else
        Sleep(5000)
    EndIf
WEnd

func startWF()
    $STARTTIME = _DateAdd( 'h' ,4, _NowCalc()) ;Adds 2 Hours to our Start Time
    $STARTTIME = _DateTimeFormat($STARTTIME,3) ;Formats $StartTime to PC's Time format
    GUICtrlSetData ($TimeLbl, ($STARTTIME))
    GUICtrlSetData ($DateLbl, ($STARTDATE))
    MsgBox( 4096, "", "Start Time is: " & $STARTTIME & @lf & _nowdate() )
EndFunc

Func endWF()
  MsgBox(0, "GUI Event", "Stopping Auto Warfields!")
  Exit
EndFuncEndFunc

I did up a quick UI so I could see how it changed

Edited by SiliconeClone
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...