Jump to content

How to set While loop until the time is not $time yet


inna
 Share

Recommended Posts

Hi,

This is my code:

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

$hour = @HOUR
$min  = @MIN
$sec  = @SEC
$msec = @MSEC
While Not $hour = 13 And $min = 19 And $sec = 30 And 599 < $msec And $msec < 999
        Run('chrome.exe')
        WinActivate('Google Chrome')
        Send('{F12}')
        ConsoleWrite($hour & ':' & $min & ':' & $sec & '.' & $msec & @CRLF) ; *** just for test
        Sleep(10000)
WEnd

What I want is until my time is not yet reached to 13:19:30, while loop continues and wait for that time (suppose current time is 13:15:00);

I did test this code with "Not" and without it, but as soon as I run the code, it exits successfully.

I'm not sure what is wrong logically with the code.

I also tested this but it runs forever:

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

$hour = @HOUR
$min  = @MIN
$sec  = @SEC
$msec = @MSEC
While 1
    if $hour = 13 And $min = 29 And $sec = 10 And 599 < $msec And $msec < 999 Then
        Run('chrome.exe')
        WinActivate('Google Chrome')
        Send('{F12}')
        ConsoleWrite($hour & ':' & $min & ':' & $sec & '.' & $msec & @CRLF) ; *** just for test
    EndIf
WEnd

Is it right?

I just want to press one time F12, writes the time on console (it's just for test) and then breaks the loop and exit the program.

Link to comment
Share on other sites

In your first example code, Not applies to $hour = 13.

Leave msec out, you don't need that and make it simple:

While _NowTime() < "13:19:30"
    Sleep(100)
WEnd

 

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

  • Developers
55 minutes ago, inna said:

Thanks, but I need milliseconds.

Why? That won't be that accurate anyway and can't really see why that would be needed.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

It is because you are reading time outside the loop.  And Not only applies to $hour (see operator precedence in help file).

Try this instead :

#include <Date.au3>

Local $tTime
While True
  $tTime = _Date_Time_GetLocalTime()
  If $tTime.hour = 7 And $tTime.minute = 59 And $tTime.second = 30 And $tTime.mseconds > 599 And $tTime.mseconds < 999 Then
    ; do what you need to do here
    ConsoleWrite("Time achieved" & @CRLF)
    Sleep(500) ; make sure you only run it once
  EndIf
  Sleep(100) ; so as not overload CPU
WEnd

 

Link to comment
Share on other sites

2 minutes ago, Jos said:

Why? That won't be that accurate anyway and can't really see why that would be needed.

Because there's some reservation that requires to hit the button, and milliseconds is important there due to high traffic of the other people like me try to reserve.

Link to comment
Share on other sites

38 minutes ago, Nine said:

It is because you are reading time outside the loop.  And Not only applies to $hour (see operator precedence in help file).

Try this instead :

#include <Date.au3>

Local $tTime
While True
  $tTime = _Date_Time_GetLocalTime()
  If $tTime.hour = 7 And $tTime.minute = 59 And $tTime.second = 30 And $tTime.mseconds > 599 And $tTime.mseconds < 999 Then
    ; do what you need to do here
    ConsoleWrite("Time achieved" & @CRLF)
    Sleep(500) ; make sure you only run it once
  EndIf
  Sleep(100) ; so as not overload CPU
WEnd

 

It runs my task but loop does not break.

This is my code (it finally exits Chrome):

#include <Date.au3>

Local $tTime
While True
  $tTime = _Date_Time_GetLocalTime()
  If $tTime.hour = 17 And $tTime.minute = 06 And $tTime.second = 25 And $tTime.mseconds > 599 And $tTime.mseconds < 999 Then
    Run('chrome.exe')
    WinActivate('Google Chrome')
    Sleep(500)
    Send('{F12}')
    Opt("SendKeyDownDelay", 1000)
    Send("{ALT down}{F4}")
    Send('{alt up}')
    ConsoleWrite("Time achieved" & @CRLF)
    Sleep(500) ; make sure you only run it once
  EndIf
  Sleep(100) ; so as not overload CPU
WEnd

And this is the Console's output:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\Saeed\Desktop\test.au3" /UserParams    
+>17:06:20 Starting AutoIt3Wrapper (21.316.1639.1) from:SciTE.exe (4.4.6.0)  Keyboard:00000409  OS:WIN_10/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\Saeed\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\Saeed\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.16.1)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\Saeed\Desktop\test.au3
+>17:06:20 AU3Check ended.rc:0
>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\Saeed\Desktop\test.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
Time achieved

Current local time is 17:10.

Link to comment
Share on other sites

  • Developers
2 minutes ago, inna said:

It runs my task but loop does not break.

Then add an ExitLoop at the point you want to break the loop... ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

3 hours ago, Jos said:

Then add an ExitLoop at the point you want to break the loop... ;) 

Thanks, I added and now it breaks the loop after one iteration.

I just decided to add InputBox like this, but the loop does not start.

I intend to take time from myself instead of editing the code (I want to compile an EXE so that I can give the app time to start) and then does what I want (in this example it is opening a Chrome browser and then exit):

#include <Date.au3>

Local $tTime
$tTime = _Date_Time_GetLocalTime()
Local $hour = InputBox('OK', 'Enter Hour (Between 0-24):')
Local $minute = InputBox('OK', 'Enter Minutes (Between 0-59):')
Local $second = InputBox('OK', 'Enter Seconds (Between 0-59):')
Local $millisecond = InputBox('OK', 'Enter MilliSeconds (Between 0-999):')
$hour = Int($hour)
$minute = Int($minute)
$second = Int($second)
$millisecond = Int($millisecond)
;MsgBox($MB_OKCANCEL, 'The Given Values', 'The entered time is ' & $hour & ':' & $minute & ':' & $second & ':' & $millisecond)
ConsoleWrite('The entered time is ' & $hour & ':' & $minute & ':' & $second & ':' & $millisecond)

While True
    If ($hour) AND ($minute) AND ($second) AND ($millisecond) Then
    Run('chrome.exe')
    WinActivate('Google Chrome')
    Sleep(500)
    Send('{F12}')
    Opt("SendKeyDownDelay", 1000)
    Send("{ALT down}{F4}")
    Send('{alt up}')
    ConsoleWrite("Time achieved" & @CRLF)
    ExitLoop
  EndIf
WEnd

I know there's something wrong with my If, but I cannot find what's wrong with it.

Link to comment
Share on other sites

  • Developers

Come on.. that can't be your best try seeing the original working one, so try again and this time think a little about it. ;)

Post your code when it doesn't work, but it has to be a bit more serious that the last.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...