Jump to content

Can't stop a loop


Recommended Posts

Hello,

I am having a problem, here. When the toggle is triggered 'ON', it works great.

My problem is that I can't seem to get it to turn OFF. It is persistently looping the 'ON' actions and completely ignoring my command to turn the commands off.

Any help? Thanks :]

-Chris

#include <GUIConstants.au3>
#include <Misc.au3>
Opt("TrayIconHide", 1)
$dll = DllOpen("user32.dll")

;Start GUI
$Form1 = GUICreate("Driller", 139, 63, 193, 63)
$Label1 = GUICtrlCreateLabel("Press ` to start/stop drilling.", 3, 10, 135, 17)
$Label2 = GUICtrlCreateLabel("Time to Drill(in milliseconds)", 3, 25, 200, 17)
$Input1 = GUICtrlCreateInput("9500", 3, 40, 100, 20)
GUISetState(@SW_SHOW)
;End GUI

Global $Toggle = '', $Drilling = ''
Func Drill()
    $Time = GUICtrlRead($Input1)
    If  $Toggle = 'ON' and $Drilling = 'ON' Then
        Sleep(700)
        Send ("{A Down}")
        Send ("{LEFT Down}")
        Sleep($Time)
        Send ("{LEFT Up}")
        Send ("{A Up}")
        $Drilling = 'OFF'
    EndIf
EndFunc

Func MoveUp()
    If $Toggle = 'ON' and $Drilling = 'OFF' Then
        Sleep(1000)
        Send ("{UP Down}")
        Sleep(150)
        Send("{UP Up}")
        $Drilling = 'ON'
    EndIf
EndFunc

While 1
    $nMsg = GUIGetMsg()
        If $nMsg = $GUI_EVENT_CLOSE Then
            Exit
        EndIf
    If _IsPressed("C0", $dll) And $Toggle <> 'ON' Then
        Sleep(100)
        $Toggle = 'ON'
        $Drilling = 'ON'
        ToolTip("Toggle ON", 0, 0)
        Sleep(400)
        ToolTip( "", 0, 0)
    EndIf
        If $Toggle = 'ON' Then
            Drill()
            MoveUp()
        EndIf
    If _IsPressed("C0", $dll) And $Toggle = 'ON' Then ; <== This here's being ignored
        $Toggle = 'OFF'
        $Drilling = 'OFF'
        ToolTip("Toggle OFF", 0, 0)
        Sleep(400)
        ToolTip( "", 0, 0)
    EndIf
WEnd
Edited by Vicate
Link to comment
Share on other sites

Thanks, but I don't think that's working.

Basically I'd like that last If...Then statement to stop everything as soon as it is hit.

Release all the keypresses, end the sleep commands, everything.

Then the capability to turn it all back on.

Basically an on/off switch is what I am looking for.

The on works fine, it's just not turning off.

I appreciate the response,

-Chris

Edited by Vicate
Link to comment
Share on other sites

I dont test it but try this;...

#include <GUIConstants.au3>
#include <Misc.au3>
Opt("TrayIconHide", 1)
$dll = DllOpen("user32.dll")

;Start GUI
$Form1 = GUICreate("Driller", 139, 63, 193, 63)
$Label1 = GUICtrlCreateLabel("Press ` to start/stop drilling.", 3, 10, 135, 17)
$Label2 = GUICtrlCreateLabel("Time to Drill(in milliseconds)", 3, 25, 200, 17)
$Input1 = GUICtrlCreateInput("9500", 3, 40, 100, 20)
GUISetState(@SW_SHOW)
;End GUI

Global $Toggle = '', $Drilling = ''
Func Drill()
    $Time = GUICtrlRead($Input1)
    If  $Toggle = 'ON' and $Drilling = 'ON' Then
        Sleep(700)
        Send ("{A Down}")
        Send ("{LEFT Down}")
        Sleep($Time)
        Send ("{LEFT Up}")
        Send ("{A Up}")
        $Drilling = 'OFF'
    EndIf
EndFunc

Func MoveUp()
    While 1
    If $Toggle = 'ON' and $Drilling = 'OFF' Then
        Sleep(1000)
        Send ("{UP Down}")
        Sleep(150)
        Send("{UP Up}")
        $Drilling = 'ON'
    EndIf

    $nMsg = GUIGetMsg()
        If $nMsg = $GUI_EVENT_CLOSE Then
            Exit
        EndIf
    If _IsPressed("C0", $dll) And $Toggle <> 'ON' Then
        Sleep(100)
        $Toggle = 'ON'
        $Drilling = 'ON'
        ToolTip("Toggle ON", 0, 0)
        Sleep(400)
        ToolTip( "", 0, 0)
    EndIf
        If $Toggle = 'ON' Then
            Drill()
            MoveUp()
        EndIf
    If _IsPressed("C0", $dll) And $Toggle = 'ON' Then ; <== This here's being ignored
        $Toggle = 'OFF'
        $Drilling = 'OFF'
        ToolTip("Toggle OFF", 0, 0)
        Sleep(400)
        ToolTip( "", 0, 0)
    EndIf
WEnd
EndFunc
I read ... I update ... I learn ...
Link to comment
Share on other sites

  • Moderators

Vicate,

Take a look at this. I have seriously modified your script because I could not see how to make it work as it was.

You now have a couple of HotKeys to Toggle and Exit. I have added a couple of labels, a few ConsoleWrites and some Sleep(500) lines so you can follow what is going on - just delete those lines if/when you are happy: :idea:

#include <GUIConstants.au3>
#include <Misc.au3>

;Opt("TrayIconHide", 1)
$dll = DllOpen("user32.dll")

HotKeySet("`", "_Switch")
HotKeySet("{ESC}", "On_Exit")

Global $Toggle = 'OFF', $Drilling = 'OFF'

;_Switch GUI
$Form1 = GUICreate("Driller", 239, 163, 193, 63)
$Label1 = GUICtrlCreateLabel("Press ` to _Switch/stop drilling.", 3, 10, 135, 17)
$Label2 = GUICtrlCreateLabel("Time to Drill (in milliseconds)", 3, 25, 200, 17)
$Input1 = GUICtrlCreateInput("9500", 3, 40, 100, 20)

$hDrilling_Label = GUICtrlCreateLabel("Drilling = " & $Drilling, 10, 130, 100, 20)
$hToggle_Label = GUICtrlCreateLabel("Toggle = " & $Toggle, 110, 130, 100, 20)

GUISetState(@SW_SHOW)
;End GUI

While 1

    If GUIGetMsg() = $GUI_EVENT_CLOSE Then
        Exit
    EndIf

    If $Toggle = 'ON' Then
        If $Drilling = "ON" Then
            ConsoleWrite("Drilling" & @CRLF)
            Drill()
            Sleep(500)
        Else
            ConsoleWrite("Moving up" & @CRLF)
            MoveUp()
            Sleep(500)
        EndIf
    EndIf


WEnd

Func Drill()
    $Time = GUICtrlRead($Input1)
    ;Sleep(700)
    ;Send ("{A Down}")
    ;Send ("{LEFT Down}")
    ;Sleep($Time)
    ;Send ("{LEFT Up}")
    ;Send ("{A Up}")
    $Drilling = 'OFF'
    GUICtrlSetData($hDrilling_Label, "Drilling = " & $Drilling)
    Sleep(500)
EndFunc   ;==>Drill

Func MoveUp()
    ;Sleep(1000)
    ;Send ("{UP Down}")
    ;Sleep(150)
    ;Send("{UP Up}")
    $Drilling = 'ON'
    GUICtrlSetData($hDrilling_Label, "Drilling = " & $Drilling)
    Sleep(500)
EndFunc   ;==>MoveUp

Func _Switch()

    If $Toggle <> 'ON' Then
        ;Sleep(100)
        $Toggle = 'ON'
        GUICtrlSetData($hToggle_Label, "Toggle = " & $Toggle)
        $Drilling = 'ON'
        GUICtrlSetData($hDrilling_Label, "Drilling = " & $Drilling)
        Sleep(500)
        ;ToolTip("Toggle ON", 0, 0)
        ;Sleep(400)
        ;ToolTip( "", 0, 0)
    Else
        $Toggle = 'OFF'
        GUICtrlSetData($hToggle_Label, "Toggle = " & $Toggle)
        $Drilling = 'OFF'
        GUICtrlSetData($hDrilling_Label, "Drilling = " & $Drilling)
        Sleep(500)
        ;ToolTip("Toggle OFF", 0, 0)
        ;Sleep(400)
        ;ToolTip( "", 0, 0)
    EndIf

EndFunc

Func On_Exit()
    Exit
EndFunc

Are we getting close? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

That is actually nearly spot-on with what I was looking for, thanks!

It waits for Func Drill to finish but it isn't persistently looping, which is what I was having trouble with.

I can live if we don't get the Drill Func to stop on a dime.

Again, thank you both for your help :]

-Chris

Link to comment
Share on other sites

  • Moderators

Vicate,

As of now, you are not drilling for any length of time. How are you intending to measure a time to drill? I can think of any number of ways in which you could then stop drilling instantly.

Would you be interested in a further version of the code? :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

The reason it doesn't stop on a dime is all you are doing is stopping the function from being called over and over. It will still finish anything that has already been called. I think the reason it wasn't working before was because you are sleeping the program within the function calls and unless it detects a key at the exact moment it checks the _IsPressed() function then it won't stop. HotKeys work differently though and it is easier to set up a toggle using them.

Link to comment
Share on other sites

  • Moderators

Vicate,

My apologies, there was a Sleep($Time) there.

Change the code I posted above by replacing this function: :idea:

Func Drill()
    $Time = GUICtrlRead($Input1)
    ;Sleep(700)
    ;Send ("{A Down}")
    ;Send ("{LEFT Down}")
    $iBegin = TimerInit()
    While TimerDiff($iBegin) < $Time
        Sleep(10)
        If $Drilling = "OFF" Then
            ConsoleWrite("Stop drilling" & @CRLF)
            Return
        EndIf
    WEnd
    ;Send ("{LEFT Up}")
    ;Send ("{A Up}")
    $Drilling = 'OFF'
    GUICtrlSetData($hDrilling_Label, "Drilling = " & $Drilling)
    Sleep(1000)
EndFunc   ;==>Drill

The slight pause you see is mainly due to the Sleep statements I have added. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

@ShawnW I see; I will keep that in mind, thanks :]

@M23 Yeah, it's drilling for the duration indicated in $Time, which is reading from $Input1.

By default it is 9500 milliseconds but can be set accordingly.

It seems to be working just the way I'd like it to.

I don't have any intentions of changing/bettering it, it is fine as-is.

It was just designed to repetitively perform a task that would otherwise be tedious.

Response to above post: I got rid of your added sleep statements; it's all good :]

Thank you all for your help!

I'd like to help people in some threads but I'm not yet savvy enough to be of any assistance, haha.

I really appreciate it.

-Chris

Edited by Vicate
Link to comment
Share on other sites

i had the same problem yesterday an i have made this :idea: maybe u can use it

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
$dll = DllOpen("user32.dll")
$Form1 = GUICreate("Test form", 208, 71, 311, 275)
$Input1 = GUICtrlCreateInput("Input1", 56, 24, 81, 21) ;- Enter number 1 = 1 sec
GUISetState(@SW_SHOW)





$timer = $Input1 * 10 / 1000
$switch = "off"
$timer_step = 0
$timecount = 0
While 1
    $nMsg = GUIGetMsg()
        If _IsPressed("10", $dll) Then
    $switch = "on"
EndIf
;~  Timer
if $switch = "on" Then
    sleep(100)
    $timer_step = $timer_step + 1
    if $timer_step = 10 Then
        $timecount = $timecount + 1
        $timer_step = 0
        if GUICtrlRead($Input1) = $timecount Then
            $switch = "off"
            MsgBox("","","done")
        EndIf
    EndIf
EndIf
If _IsPressed("1B", $dll) Then  ;~   ESC to exit
    Exit
EndIf
WEnd
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...