Jump to content

Stopping the script


Kyou
 Share

Recommended Posts

This is a part of the script I am using now. This seems to work but I cannot make the scipt stop if I click the stop buttton.

Func _Action()
    Switch GUICtrlRead($actionitem)
        ; If button reads "Start"
        Case "Start"
            GUICtrlSetData($actionitem, "Stop")  ; change button text
            ; whatever you need as start code here
            ; MsgBox(0, "", "Started") ; Just as an indication
               Sleep (3000)
               ControlSend ( $title, "", 0, "This is some text")
               Sleep(Random(3000, 9000, 1))
               ControlSend ( $title, "", 0, "This is some text")
               Sleep(Random(1000, 5000, 1))
        ; If button reads "Stop"
        Case "Stop"
            GUICtrlSetData($actionitem, "Start")  ; change button text
            ; whatever you need as stop code here
           ; MsgBox(0, "", "Stopped") ; Just as an indication
    EndSwitch
EndFunc   ;==>_Action

I wanted to stop the script when a user clicks on the stop button. How can I implement this? Thank you so much!

Link to comment
Share on other sites

Have a look in the help, the example for GUICtrlCreateProgress show's good technic to start and stop.

Doesn't seem to help me. I think what I want is more like a while statement which I can use inside this current action I am using..

Link to comment
Share on other sites

Because you only postet a snipet i show you this way. When this isn't enough for your AutoIt-skills post a runable script.

Here's my entire working code:

#include <GUIConstants.au3>
#include <Constants.au3>
$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0
While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend
Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
 EndFunc
If $fullTest = 1 Then
    MsgBox(0, "WINDOW Detection Success", "Press Ok to continue")
    GUICreate("my gui", 300, 150) ; will create a dialog box that when displayed is centered
GUISetState (@SW_SHOW)    ; will display an empty dialog box
Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$Button_1 = GUICtrlCreateButton ("Start",  50, 90, 100)
$Button_2 = GUICtrlCreateButton ( "Stop / Exit",  0, -1)

GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
GuICtrlSetOnEvent($Button_2,"Quit")
GuiCtrlSetOnEvent($Button_1,"DoScript")

Else
    MsgBox(0, "WINDOW Detection Failed", "Please run WINDOW first.")
 EndIf

Func DoScript()
    Sleep (3000)
    ControlSend ( $title, "", 0, "{F12}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Del}")
    Sleep(Random(1000, 5000, 1))
EndFunc

Func Quit()
    Exit
 EndFunc

What I wanted to do is to incorporate this:

#include <GUIConstantsEx.au3>

GUICreate("GUICtrlSetData", 200,100)
GUISetState(@SW_SHOW)

$button = GUICtrlCreateButton("Start",60, 30, 80, 20)
$count = 0

While 1
    Switch GUIGetMsg()
    Case $button
        If $count = 1 Then
            GUICtrlSetData($button, "Start")
            $count = 0
        ElseIf $count = 0 Then
            GUICtrlSetData($button, "Stop")
            $count = 1
        EndIf
    Case $GUI_EVENT_CLOSE
        Exit
    EndSwitch
WEnd

I wanted to run this particular part of the script:

while 1
    sleep (3000)
    ControlSend ( $title, "", 0, "{F5}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Ins}")
    Sleep(Random(1000, 5000, 1))
 wend

When you press start and stop the script when you click stop.

 

Thank you so much for guiding me.

Link to comment
Share on other sites

To stop a function which was started using OnEvent mode the only 2 ways are Hotkey and/or GuiRegisterMsg

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0

While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend

If $fullTest = 1 Then
    MsgBox(0, "WINDOW Detection Success", "Press Ok to continue")
    GUICreate("my gui", 300, 150) 
    GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
    $Button_1 = GUICtrlCreateButton ("Start",  50, 90, 100)
    GuiCtrlSetOnEvent($Button_1,"DoScript")
    $Button_2 = GUICtrlCreateButton ( "Stop / Exit",  0, -1)
    GuiCtrlSetOnEvent($Button_2,"Quit")
    GUISetState (@SW_SHOW)   
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")  ;<<<<<<<<<<<<<<<<<<<<<
Else
    MsgBox(0, "WINDOW Detection Failed", "Please run WINDOW first.")
 EndIf

While 1
   Sleep(10)
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $Button_2 Then Exit
     Return $GUI_RUNDEFMSG
EndFunc 

Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
EndFunc

Func DoScript()
    Sleep (3000)
    ControlSend ( $title, "", 0, "{F12}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Del}")
    Sleep(Random(1000, 5000, 1))
EndFunc

Func Quit()
    Exit
 EndFunc

 

Link to comment
Share on other sites

To stop a function which was started using OnEvent mode the only 2 ways are Hotkey and/or GuiRegisterMsg

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0

While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend

If $fullTest = 1 Then
    MsgBox(0, "WINDOW Detection Success", "Press Ok to continue")
    GUICreate("my gui", 300, 150) 
    GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
    $Button_1 = GUICtrlCreateButton ("Start",  50, 90, 100)
    GuiCtrlSetOnEvent($Button_1,"DoScript")
    $Button_2 = GUICtrlCreateButton ( "Stop / Exit",  0, -1)
    GuiCtrlSetOnEvent($Button_2,"Quit")
    GUISetState (@SW_SHOW)   
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")  ;<<<<<<<<<<<<<<<<<<<<<
Else
    MsgBox(0, "WINDOW Detection Failed", "Please run WINDOW first.")
 EndIf

While 1
   Sleep(10)
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $Button_2 Then Exit
     Return $GUI_RUNDEFMSG
EndFunc 

Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
EndFunc

Func DoScript()
    Sleep (3000)
    ControlSend ( $title, "", 0, "{F12}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Del}")
    Sleep(Random(1000, 5000, 1))
EndFunc

Func Quit()
    Exit
 EndFunc

 

Hi mikell,

Thank you so much for this code. I have learned a lot with these. I just have a few questions.

1. Is there a way for me to just pause or stop the running script when I click the 'stop' button and NOT completely quit or exit the application?

2.  Is there a way to for me to make the start button change to stop after clicking it? In this way, the user will know if the script is indeed started or stopped. Just like this code:

#include <GUIConstantsEx.au3>

GUICreate("GUICtrlSetData", 200,100)
GUISetState(@SW_SHOW)

$button = GUICtrlCreateButton("Start",60, 30, 80, 20)
$count = 0

While 1
    Switch GUIGetMsg()
    Case $button
        If $count = 1 Then
            GUICtrlSetData($button, "Start")
            $count = 0
        ElseIf $count = 0 Then
            GUICtrlSetData($button, "Stop")
            $count = 1
        EndIf
    Case $GUI_EVENT_CLOSE
        Exit
    EndSwitch
WEnd

Thank you so much for your reply

Link to comment
Share on other sites

Possible but a bit more complicated because you need then to manage the Sleep() differently, using a global var ($stop in the code below)
I added an input just to show when the DoScript() func is running or not

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0
$stop = 1  ;<<<<<<<<<<<<<<<<<<<<<<<<<

While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend

If $fullTest = 1 Then
    MsgBox(0, "WINDOW Detection Success", "Press Ok to continue")
    GUICreate("my gui", 300, 150) 
    $label = GUICtrlCreateInput ("test", 10, 10, 50, 20)
    GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
    $button = GUICtrlCreateButton ("Start",  50, 90, 100, 25)
    GuiCtrlSetOnEvent($button,"DoScript")
    GUISetState (@SW_SHOW)   
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")  ;<<<<<<<<<<<<<<<<<<<<<
Else
    MsgBox(0, "WINDOW Detection Failed", "Please run WINDOW first.")
 EndIf

While 1
   Sleep(10)
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $button Then 
          $stop = not $stop
          If $stop = 0 Then GUICtrlSetData($button, "Stop")
          If $stop = 1 Then GUICtrlSetData($button, "Start")
     EndIf
     Return $GUI_RUNDEFMSG
EndFunc 

Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
EndFunc

Func DoScript()
    For $i = 1 to 300  ;<<<<<<<<<< means Sleep(3000)
        Sleep (10)
        If $stop = 1 Then Return
        GuiCtrlSetData($label, $i)
    Next
    ControlSend ( $title, "", 0, "{F12}")
    $a = Random(300, 900, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
    Next
    ControlSend ( $title, "", 0, "{Del}")
    $a = Random(100, 500, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
    Next
EndFunc

Func Quit()
    Exit
 EndFunc

Edit
PLease note that the window can't be closed while the DoScript() func is running, you must click "stop" first. But if necessary  this can be solved by adding a GuiRegisterMsg($WM_SYSCOMMAND, ...)

GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND") 

Func WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0xFFF0) = 0xF060 Then Exit   ;$SC_CLOSE = 0xF060
    Return $GUI_RUNDEFMSG
EndFunc

 

Edited by mikell
Link to comment
Share on other sites

Yes don't use the GuiOnEventMode. May be a look in the Help-File => Gui Reference => Gui Concepts helps you to understand the basics of AutoIt.

Btw. Which Application want you automate whith

while 1
    sleep (3000)
    ControlSend ( $title, "", 0, "{F5}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Ins}")
    Sleep(Random(1000, 5000, 1))
 wend

 

Link to comment
Share on other sites

Yes don't use the GuiOnEventMode. May be a look in the Help-File => Gui Reference => Gui Concepts helps you to understand the basics of AutoIt.

Btw. Which Application want you automate whith

while 1
    sleep (3000)
    ControlSend ( $title, "", 0, "{F5}")
    Sleep(Random(3000, 9000, 1))
    ControlSend ( $title, "", 0, "{Ins}")
    Sleep(Random(1000, 5000, 1))
 wend

 

Its an application I am using for my work.

 

Possible but a bit more complicated because you need then to manage the Sleep() differently, using a global var ($stop in the code below)
I added an input just to show when the DoScript() func is running or not

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0
$stop = 1  ;<<<<<<<<<<<<<<<<<<<<<<<<<

While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend

If $fullTest = 1 Then
    MsgBox(0, "WINDOW Detection Success", "Press Ok to continue")
    GUICreate("my gui", 300, 150) 
    $label = GUICtrlCreateInput ("test", 10, 10, 50, 20)
    GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
    $button = GUICtrlCreateButton ("Start",  50, 90, 100, 25)
    GuiCtrlSetOnEvent($button,"DoScript")
    GUISetState (@SW_SHOW)   
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")  ;<<<<<<<<<<<<<<<<<<<<<
Else
    MsgBox(0, "WINDOW Detection Failed", "Please run WINDOW first.")
 EndIf

While 1
   Sleep(10)
Wend


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $button Then 
          $stop = not $stop
          If $stop = 0 Then GUICtrlSetData($button, "Stop")
          If $stop = 1 Then GUICtrlSetData($button, "Start")
     EndIf
     Return $GUI_RUNDEFMSG
EndFunc 

Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
EndFunc

Func DoScript()
    For $i = 1 to 300  ;<<<<<<<<<< means Sleep(3000)
        Sleep (10)
        If $stop = 1 Then Return
        GuiCtrlSetData($label, $i)
    Next
    ControlSend ( $title, "", 0, "{F12}")
    $a = Random(300, 900, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
    Next
    ControlSend ( $title, "", 0, "{Del}")
    $a = Random(100, 500, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
    Next
EndFunc

Func Quit()
    Exit
 EndFunc

Edit
PLease note that the window can't be closed while the DoScript() func is running, you must click "stop" first. But if necessary  this can be solved by adding a GuiRegisterMsg($WM_SYSCOMMAND, ...)

GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND") 

Func WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0xFFF0) = 0xF060 Then Exit   ;$SC_CLOSE = 0xF060
    Return $GUI_RUNDEFMSG
EndFunc

 

This is exactly what I need. I modified it a bit adding While to it to continously loop while its activated. Also, the second code, I didnt use it as I dont know yet where to add those. I am learning. I have another question thought. Here is the code I worked with:

#include <GUIConstants.au3>
#include <Constants.au3>

Opt("GUICoordMode",2)
Opt("GUIOnEventMode",1)

$title = "WINDOW"
$text=""
$fullTest = WinExistsExact($title, $text)
$PASSWORD = "PASS"
$retryCount = 0
$stop = 1  ;<<<<<<<<<<<<<<<<<<<<<<<<<

While 1
    if $retryCount > 0 then Exit
    $input = InputBox("Password Protected", "Enter the password to continue", "", "*")
    If @error Or $input <> $PASSWORD Then
        MsgBox(4096,"Error", "Incorrect Password")
        $retryCount = $retryCount + 1
    Else
        ;MsgBox(4096,"Success", "Password Accepted")
        ExitLoop
    EndIf
 Wend

If $fullTest = 1 Then
    ;MsgBox(0, "WINDOW", "Press Ok to continue")
    GUICreate("WINDOW", 241, 78)
    ;$label = GUICtrlCreateInput ("test", 10, 10, 50, 20)
    GuiSetOnEvent($GUI_EVENT_CLOSE,"Quit")
    $button = GUICtrlCreateButton ("Start",  18, 31, 100, 25)
    $button1 = GUICtrlCreateButton ("Hide",  128, 31, 91, 25) ; <----- THIS IS THE BUTTON
    GuiCtrlSetOnEvent($button,"DoScript")
    GUISetState (@SW_SHOW)
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")  ;<<<<<<<<<<<<<<<<<<<<<
Else
    MsgBox(0, "WINDOW", "You need to run WINDOW client first.")
 EndIf

While 1
   Sleep(10)
Wend

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $button Then
          $stop = not $stop
          If $stop = 0 Then GUICtrlSetData($button, "Stop")
          If $stop = 1 Then GUICtrlSetData($button, "Start")
     EndIf
     Return $GUI_RUNDEFMSG
EndFunc

Func WinExistsExact($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHEXACT )
    $res = WinExists($title, $text)
    AutoItSetOption( "WinTitleMatchMode", $OPT_MATCHSTART )
    Return $res
EndFunc

Func DoScript()
   While 1
    For $i = 1 to 300
        Sleep (10)
        If $stop = 1 Then Return
       ; GuiCtrlSetData($label, $i)
    Next
    ControlSend ( $title, "", 0, "{F12}")
    $a = Random(300, 900, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
    Next
    ControlSend ( $title, "", 0, "{Del}")
    $a = Random(100, 500, 1)
    For $i = 1 to $a
        Sleep (10)
        If $stop = 1 Then Return
        Next
   WEnd
EndFunc

Func Quit()
    Exit
 EndFunc

As you can see, I added a line that should make a second button while will minimize on tray when clicked. Similar to the code below:

#NoTrayIcon
#include <guiconstantsex.au3>
#include <constants.au3>
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)
; Create tray menu items and set the events
$About = TrayCreateItem("About")
TrayItemSetOnEvent($About, "_About")
TrayCreateItem("") ; Create a separator line.
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($Exit, "_Exit") 
; Set the action if you left click on the tray icon
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "SpecialEvent")
; Show the menu if the icon is right clicked
TraySetClick(8)
Global $MainGui = GUICreate("TestGUI", 392, 316, -1, -1)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_MINIMIZE
            ; Hide the GUI
            GUISetState(@SW_HIDE)
            ; Show the icon
   TraySetState(1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
Func SpecialEvent()
    ; Show the GUI
    GUISetState(@SW_SHOW)
    GUISetState(@SW_RESTORE)
    ; Hide the icon
    TraySetState(2)
EndFunc   ;==>SpecialEvent
Func _About()
    MsgBox(0,0,"Test")
EndFunc
Func _Exit()
    Exit
EndFunc

Is it possible to actually add a second button to that start button and named it Hide so it will minimize on tray?

I am learning so much in this process. I hope you could add more comments to the lines so I could understand it more. Thank you so much for your reply.

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