ZeroByDevide

while loop

5 posts in this topic

#1 ·  Posted

so right now i have a small project and i wanted to print this text out every second.

i have made a random number for the sleep function. but when i want to print out with the while loop it just doesn't work.

Run("notepad.exe")
Sleep(1000)
Local $rndSleep = Int (Random(180000,240000,1000))

While $rndSleep <> 0
   $rndSleep - 1
   If Mod ( $rndSleep, 1000 ) == 0 Then
   Send("This note will show the sleeptime before closing the tabs, you got " & $rndSleep & " seconds left.")
   EndIf
WEnd

plis halp me :D

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

ZeroByDevide,

This works for me:

Local $rndSleep = Int  (Random(180,240,1))

$iSec = @SEC

While 1
    If $iSec <> @SEC Then
        $iSec = @SEC
        $rndSleep -= 1
        If $rndSleep Then
            ConsoleWrite("This note will show the sleeptime before closing the tabs, you got " & $rndSleep & " seconds left." & @CRLF)
        Else
            ExitLoop
        EndIf
    EndIf
    ; Important to let the CPU breathe
    Sleep(10)
WEnd

M23

Edited by Melba23
Amended code
1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#3 ·  Posted

THanks!!

Share this post


Link to post
Share on other sites

#4 ·  Posted

There are  few errors in the original code:

; this won't do anything
$rndSleep - 1

;you need
$rndSleep -= 1

This might be of help:
https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm

;this will work
If Mod ( $rndSleep, 1000 ) == 0 Then

;but you should use
If Mod ( $rndSleep, 1000 ) = 0 Then
;as == is for comparing strings, so there is extra work being done to convert both sides of the comparison to strings

Other problems with the code are:

1) It won't display the text every second, it will display every 1000 loops of the code, which will depend on the CPU speed.

2) There's no carriage return, so the text will display all on the same (long) line. Add & @CRLF  to the send command.

3) If you click on a different window, that window will receive the text, this could cause problems :D

Melbas solution has a problem, it doesn't stop at 0

Share this post


Link to post
Share on other sites

#5 ·  Posted

weirddave,

Quote

Melbas solution has a problem, it doesn't stop at 0

It does now - thanks for the prompt!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

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

  • Similar Content

    • Warriorfoe
      Nesting If then Statements
      By Warriorfoe
      Okay so here's the problem.
      I have a script I'm testing out where I want it to search for a certain color and if it finds that certain color I want it to check for a second color. I only want it to check for the second color after checking for the first color. If it finds the first color it will check for the second color and whether or not that second color is present it will cause certain actions to be undertaken. Well at least that's what I want it to do. I want this all to be done in a loop where it's constantly checking for the first color. However, once it does find the first color and it executes one of the actions regarding the second, I want the program to quit. I'm having trouble figuring out how to nest conditional statements here. If anyone could help me out it would be much appreciated.
      HotKeySet("{ESC}", "Terminate") HotKeySet("!c", "getColor") HotKeySet("!l", "ClickPositions") Global $color Global $interval $interval = "10000" MsgBox (0, "Starting Up", "Starting, use ESC to quit, Alt + C to get color, and Alt + L to begin Test") Func Terminate() Exit 1 EndFunc Func ClickPositions() MsgBox(0, "Beginning Test", "Hope to god this works") While 1 $Coords = PixelSearch(1006, 48, 1074, 79, 0xECECEC) $1stcoords = PixelSearch(930, 621, 1066, 649, 0x8A211E) $point = MouseGetPos() $posx = Random(930, 1066) $posy = Random(621, 649) $clicklogx = Random(1076, 1093) $clicklogy = Random(38, 54) If IsArray($Coords) Then ;check to see if the first color is there If IsArray($1stcoords) Then ;if it is check for the second color and if that one is there: MouseClick( "left", $point[0], $point[1], 1) If IsArray(<>$1stcoords) Then ;if the second color is not there: MouseClick("left", $clicklogx, $clicklogy, 1, Random(1000, 2500)) Sleep(3000) MouseClick( "left" , $posx, $posy, 1, Random ( 1000 , 2500)) EndIf EndIf EndIf Sleep(2000) WEnd EndFunc While 1 Sleep(250) WEnd  
    • Masum
      If conditions not working
      By Masum
      Hi all,
      I'm going crazy while I try and work out why a fairly simply if condition isn't working. I will try and explain it clearly, in the hope someone can find out the cause. The portion of the script for these if conditions is copied in below. The first PixelGetColor func is denoted in Screenshot 1, the RED dot signifies where the color is extracted from. The second PixelGetColor func is denoted in Screenshot 2, the YELLOW dot signifies where the color is extracted from. The script is tested for 3 scenarios, where 1. no resource is found (Screenshot 1), 2. multiple resources are found (Screenshot 2), and 3. only 1 resource is found (Screenshot 3). This application is being run via RDP hence I can't access the controls, and therefore relying on the pixel colours. The issue is, for all three scenarios, only the first If condition's first result is shown, even though the actual program shows the 3 screenshots for each of the three scenarios. Any ideas where I'm going wrong?
      If PixelGetColor(697, 446, 0xF0F0F0) Then ;If resource is not found, cancel out
              Sleep(200)
              MsgBox(64, 'Error', 'Resource is not found, therefore this pool will not be saved and will cancel out. Click OK to continue.')
              _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Fail', 'I' & $ColumnIRow)
              _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Resource not found. ' & $Notes, 'J' & $ColumnIRow)
              Sleep(500)
              WinActivate('Scheduling Help')
              Sleep(500)
              Send('{ENTER}') ;Click OK
              Sleep(500)
              Send('{SHIFTDOWN}{TAB}{SHIFTUP}{ENTER}') ;Click Cancel
              Sleep(500)
              Send('{SHIFTDOWN}{TAB}{TAB}{TAB}{TAB}{SHIFTUP}{ENTER}') ;Click Close
              Sleep(500)
              WinActivate('Scheduling: DB Flex Rule Tool - Inbox Pool Routing')
              Sleep(500)
              Send('{TAB}{ENTER}') ;Click Yes
              Sleep(500)
              WinActivate('Rules for Pool')
              Sleep(500)
              Send('{TAB}{TAB}{TAB}{ENTER}') ;Click Cancel
              Sleep(500)
              MouseClick('Primary', 1259, 653, 1) ;Click Cancel
          Else
              Send('{TAB}{TAB}{DOWN}') ;Find resource
              If PixelGetColor(725, 383, 0x3399FF) Then ;If there are multiple resources, cancel out
                  Sleep(200)
                  MsgBox(64, 'Error', 'There are multiple resources, therefore this pool will not be saved and will cancel out. Click OK to continue.')
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Redo', 'I' & $ColumnIRow)
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Multiple resources. ' & $Notes, 'J' & $ColumnIRow)
                  Sleep(500)
                  WinActivate('Scheduling Help')
                  Sleep(500)
                  Send('{TAB}{TAB}{ENTER}') ;Click Cancel
                  Sleep(500)
                  Send('{SHIFTDOWN}{TAB}{TAB}{TAB}{TAB}{SHIFTUP}{ENTER}') ;Click Close
                  Sleep(500)
                  WinActivate('Scheduling: DB Flex Rule Tool - Inbox Pool Routing')
                  Sleep(500)
                  Send('{TAB}{ENTER}') ;Click Yes
                  Sleep(500)
                  WinActivate('Rules for Pool')
                  Sleep(500)
                  Send('{TAB}{TAB}{TAB}{ENTER}') ;Click Cancel
                  Sleep(500)
                  MouseClick('Primary', 1259, 653, 1) ;Click Cancel
              Else ;Select the resource
                  Send('{ENTER}') ;Select Order Physician
                  MsgBox(0, '', 'save button clicks now')
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Complete', 'I' & $ColumnIRow)
                  _Excel_RangeWrite($oWorkbook, 'CHFT Pools & RTE Rules', 'Success. ' & $Notes, 'J' & $ColumnIRow)
                  ;Send('{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{ENTER}') ;Click Save
              EndIf
          EndIf

    • SaeidN
      Select or Switch or If ?
      By SaeidN
      Hi,
      I want to tell pixelsearch to search for red color shades in for example 5 different x,y,h,w of the screen. If color red was not found in these 5 area, then do something.
      I wrote this, but it's working only if the first case is not found. Is select a good choice? or it's better to use switch or if or something else?
      ٍEdit: all "do something"s are same function. (search for red color untill in these 5 areas, it couldnot find red color, then if it couldn't find red color, perform that 1 function)
      Thanks
      Select Case 1 $color1 = PixelSearch(67, 614, 77, 617, 0xE62121, 10) If @error Then do something... EndIf Case 2 $color2 = PixelSearch(165, 614, 175, 617, 0xE62121, 10) If @error Then do something... EndIf Case 3 $color3 = PixelSearch(265, 614, 275, 617, 0xE62121, 10) If @error Then do something... EndIf Case 4 $color4 = PixelSearch(365, 614, 375, 617, 0xE62121, 10) If @error Then do something... EndIf Case 5 $color5 = PixelSearch(465, 614, 475, 617, 0xE62121, 10) If @error Then do something... EndIf EndSelect  
    • Jcreator
      i used while to display images but it went wrong
      By Jcreator
      i used this code to take names for a text file called data and put the info of the image and it location from the data file into the gui interface but it only detect the lines with the info and it do not show the images . here is the code 
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <AVIConstants.au3> #include <TreeViewConstants.au3> #include <GuiComboBox.au3> #include <GuiTab.au3> #include <file.au3> #include <array.au3> _Main() Func _Main() Local $idTab1 $Form1 = GUICreate("Form1", 500, 334, 240, 182) $Group1 = GUICtrlCreateGroup("Group1", 20, 12, 249, 314) $idButton6 = GUICtrlCreateButton("Info", 50, 270, 185, 49) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) Global $fileMPA = "C:\T-in\MPA\data.txt" FileOpen($fileMPA, 0) Global $line1 = FileReadLine($fileMPA , 1) $imp = 0 $idTab1 = GUICtrlCreateTab(300, -3, 180, 330) GUICtrlCreateTabItem("imglist1") GUICtrlSetTip(-1, '#Region TAB1') GUICtrlSetTip(-1, '#Region LIST') Local $imglist1 = GUICtrlCreateList("", 307, 26, 164, 304) $cline =_FileCountLines($fileMPA) While $imp < $cline $line2 = FileReadLine($fileMPA , $imp + 2) $imp = $imp + 7 if $line2 = "" then ExitLoop else GUICtrlSetData(-1, ""& $line2 &"", "") endif WEnd GUICtrlCreateTabItem("imagelist2") GUICtrlSetTip(-1, '#Region TAB2') GUICtrlCreateTabItem("imagelist3") GUICtrlSetTip(-1, '#Region TAB3') While 1 $Msg = GUIGetMsg() Select Case $Msg = $GUI_EVENT_CLOSE Exit Global $whatnow = GUICtrlRead($imglist1) Case $Msg = $idButton6 $imp2 = 0 While $imp2 < 30 $line2 = FileReadLine($fileMPA ,$imp2 + 2) $line3 = FileReadLine($fileMPA , $imp2 + 3) $line7 = FileReadLine($fileMPA , 7) $imp2 = $imp2 + 7 if $line2 = "" then ExitLoop If GUICtrlRead($imglist1) = ""& $line2 &"" Then MsgBox($MB_SYSTEMMODAL, "img info" , $line3) WEnd EndSelect $imp2 = 0 While $imp2 < 30 $line2 = FileReadLine($fileMPA ,$imp2 + 2) $line3 = FileReadLine($fileMPA , $imp2 + 3) $line7 = FileReadLine($fileMPA , 7) $imp2 = $imp2 + 7 Select Case GUICtrlRead($imglist1) = ""& $line2 &"" if $line2 = "" then ExitLoop $Pic1 = GUICtrlCreatePic(""& $line7 &"", 185, 28, 233, 230) If GUICtrlRead($imglist1) then ExitLoop EndSelect WEnd WEnd EndFunc ;==>_Main i wish some one can help me fix this without having to choose the images manually and i know that the script is messy but am still new in the world of autoit some bear with me . thanks for your time
    • TheOnlyOne
      Hotkeyset seems to stop working when i hit a while loop
      By TheOnlyOne
      So currently I have some code waiting for a status to change(there is different indicators to check if this status has changed) 
      Func oneRow($count) $globaltimer = _Timer_SetTimer($gui, 60000*10, "reset") $errTimer = TimerInit() While (1)         If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)         Sleep(500)         MouseClick("right")         Sleep(600)         If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)         If TimerDiff($errTimer) > 5000 Then             If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then                 Sleep(100)                 Send("{esc}")                 Sleep(1000)             EndIf         EndIf     WEnd .... EndFunc   ;==>oneRow The two timers is for error checking. This works just fine the problem comes when I go into the while 1 loop, my hotkeys at the top of the program seems to stop working, for an example 
      HotKeySet("{f1}", "stop") Func stop() Exit EndFunc ;==>stop Does not work at all after the while loop. The globaltimer is never called even though i tried setting the delay all the way down to 3 seconds. So I thought that maybe one of the function I use is blocking so the timer would never be called/the hotkey wouldn't work? if i make an delay just before the while loop the hotkey works just fine. Oh and the errTimer is supossed to not reset after 5 seconds but should keep making the check it does if five seconds have passed