cadjenk

Changing name of button on every press gets stuck

11 posts in this topic

Hi everyone.
I've written some code for a GUI with two buttons, a RUN button that hanges to PAUSE on press and a STOP button which exit the script.
When the buttons are pressed they trigger a hotkey whcih then triggers the desired function. This script works fine if you press the hotkeys or if the RUN button is clickd but it gets stuck in a loop whenever the PAUSE button is clicked.
Does anyone know why this happens and how I can fix it?

#include <GUIConstantsEx.au3>                       ;For GUI
Global $Pause = False

HotKeySet("^r", "_FnRunPause")
HotKeySet("^t", "_FnStop")


;Create GUI
Opt("GUIOnEventMode", 1) ;Set on event mode for GUI
$hGUI = GUICreate("PS Mining Bot", 150, 250) ;Create main GUI window
$Run = GUICtrlCreateButton("RUN", 25, 25, 100, 75) ;Add Run button to GUI
$Stop = GUICtrlCreateButton("STOP", 25, 150, 100, 75) ;Add exit button to GUI
GUISetState(@SW_SHOW, $hGUI)

;Set actions on button press
GUISetOnEvent($GUI_EVENT_CLOSE, "Stop")
GUICtrlSetOnEvent($Run, "Start")
GUICtrlSetOnEvent($Stop, "Stop")

_FnTogglePause()

;main code
While 1
    Sleep(2000)
    MsgBox(0, "", "this is where main code goes", 1)
WEnd



Func Stop()
    ControlSend($hGUI, "", "", "^t")
EndFunc

Func _FnStop()
    Exit
EndFunc

Func Start()
    ControlSend($hGUI, "", "", "^r")
EndFunc

Func _FnRunPause()
    Switch GUICtrlRead($Run)
        Case "RUN"
            GUICtrlSetData($Run, "PAUSE")
            $Pause = False
        Case "PAUSE"
            GUICtrlSetData($Run, "RUN")
            _FnTogglePause()
    EndSwitch
EndFunc

Func _FnTogglePause()
    $Pause = Not $Pause
    While $Pause
        Sleep(2000)
        MsgBox(0, "", "stuck here", 1)
    WEnd
EndFunc

Regards.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well, I must say, I tried debugging it, but it has me scratching my head.  The event is obviously stuck... but no idea why at the moment.

#include <GUIConstantsEx.au3>                       ;For GUI
Global $Pause = False

HotKeySet("^r", "_FnRunPause")
HotKeySet("^t", "_FnStop")

;Create GUI
Opt("GUIOnEventMode", 1) ;Set on event mode for GUI
$hGUI = GUICreate("PS Mining Bot", 150, 250) ;Create main GUI window
$Run = GUICtrlCreateButton("RUN", 25, 25, 100, 75) ;Add Run button to GUI
$Stop = GUICtrlCreateButton("STOP", 25, 150, 100, 75) ;Add exit button to GUI

;Set actions on button press
GUISetOnEvent($GUI_EVENT_CLOSE, "Stop")
GUICtrlSetOnEvent($Run, "Start")
GUICtrlSetOnEvent($Stop, "Stop")

GUISetState(@SW_SHOW, $hGUI)

ConsoleWrite("... no idea why I'm toggling before main loop..." & @CRLF)
_FnTogglePause()

;main code
While 1
    Sleep(2000)
    ConsoleWrite("... main loop..." & @CRLF)
WEnd

Func Stop()
    ControlSend($hGUI, "", "", "^t")
EndFunc

Func _FnStop()
    Exit
EndFunc

Func Start()
    ControlSend($hGUI, "", "", "^r")
EndFunc

Func _FnRunPause()
    ConsoleWrite("... enter _FnRunPause()..." & @CRLF)
    Switch GUICtrlRead($Run)
        Case "RUN"
            GUICtrlSetData($Run, "PAUSE")
            ConsoleWrite("... setting pause to false ..." & @CRLF)
            $Pause = False ; this makes pause true in _FnTogglePause function but you don't call it
        Case "PAUSE"
            GUICtrlSetData($Run, "RUN")
            ConsoleWrite("... going to pause toggle..." & @CRLF)
            _FnTogglePause() ; This makes pause false or true, opposite of what is set
    EndSwitch
EndFunc

Func _FnTogglePause()
    $Pause = Not $Pause
    ConsoleWrite("Enter Pause State: " & $Pause & @CRLF)
    While $Pause
        Sleep(2000)
        ConsoleWrite("... pause loop..." & @CRLF)
    WEnd
    ConsoleWrite("Exit Pause State: " & $Pause & @CRLF)
EndFunc

... I was testing on top of another script, not that it would change anything, but I'll run it in a file of its own.

Edit2:

I assumed the ControlSend() was not really for this GUI, that it was an example of some other issue you were running into, because it's just bad form to rely on the hotkeys within your own GUI when you can call the function directly from the control you're manipulating... but even replacing those, the event still sticks... so I'm stuck on baffle and no patience to debug more at the moment...

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for taking a look at this, I hope you can figure it out.
I have included comments on where you were unsure why I did something a certain way.
I have no formal coding background so I'm just trying to approach things in what I see as a logical manner. If there is a better way to go about something please let me know.

#include <GUIConstantsEx.au3>                       ;For GUI
 Global $Pause = False

HotKeySet("^r", "_FnRunPause")
HotKeySet("^t", "_FnStop") 

;Create GUI
Opt("GUIOnEventMode", 1) ;Set on event mode for GUI 
$hGUI = GUICreate("PS Mining Bot", 150, 250) ;Create main GUI window 
$Run = GUICtrlCreateButton("RUN", 25, 25, 100, 75) ;Add Run button to GUI 
$Stop = GUICtrlCreateButton("STOP", 25, 150, 100, 75) ;Add exit button to GUI 

;Set actions on button press 
GUISetOnEvent($GUI_EVENT_CLOSE, "Stop") 
GUICtrlSetOnEvent($Run, "Start")
GUICtrlSetOnEvent($Stop, "Stop") 

GUISetState(@SW_SHOW, $hGUI)

 ConsoleWrite("... no idea why I'm toggling before main loop..." & @CRLF) 
;I toggle this before the main loop so that the script starts of paused until RUN is clicked
_FnTogglePause() 

;main code
While 1
     Sleep(2000)
     ConsoleWrite("... main loop..." & @CRLF)
WEnd 

Func Stop()
     ControlSend($hGUI, "", "", "^t")
EndFunc 

Func _FnStop()
     Exit
EndFunc 

Func Start()
     ControlSend($hGUI, "", "", "^r")
EndFunc 

Func _FnRunPause()
     ConsoleWrite("... enter _FnRunPause()..." & @CRLF)
     Switch GUICtrlRead($Run)
         Case "RUN" 
             GUICtrlSetData($Run, "PAUSE")
             ConsoleWrite("... setting pause to false ..." & @CRLF)
             $Pause = False ; this makes pause true in _FnTogglePause function but you don't call it
             #cs 
                 I don't call _FnTogglePause because it is already running if the script is paused, and setting $Pause to 
                 False exits it. I could also just run _FnTogglePause without setting the state of $Pause but it didn't
                 make sense to me to call the function if it's already active. Is it better practice to do this a different way?
             #ce
         Case "PAUSE"
             GUICtrlSetData($Run, "RUN")
             ConsoleWrite("... going to pause toggle..." & @CRLF)
             _FnTogglePause() ;This makes pause false or true, opposite of what is set
     EndSwitch 
EndFunc 

Func _FnTogglePause()     
     $Pause = Not $Pause
     ConsoleWrite("Enter Pause State: " & $Pause & @CRLF)
     While $Pause
         Sleep(2000)
         ConsoleWrite("... pause loop..." & @CRLF)
     WEnd
     ConsoleWrite("Exit Pause State: " & $Pause & @CRLF)
EndFunc

Edit: I used control send because of something i read in the hotkeyset help file

 

To Send() a key combination which will trigger a HotKeySet() event, either use ControlSend() or unregister the HotKeySet() event, otherwise, the Send() event may trigger an infinite loop.

 

I didn't really understand it but I thought I'd play it safe and use controlsend.

I used hotkeys to trigger functions because as far as I know, hotkeys can interrupt a script pretty much anywhere whereas clicking a GUI button will wait for the current loop to end before taking action. I don't know where exactly I got this idea if it's wrong but pretty much iI was looking for a way to easily interrupt a long script with lots of long sleep functions in it, at any point.

Regards

Edited by cadjenk

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Func Stop()
_FnStop()
;~     ControlSend($ghGUI, "", "", "^t")
EndFunc
 
Func _FnStop()
    Exit
EndFunc
 
Func Start()
_FnRunPause()
;~     ControlSend($ghGUI, "", "", "^r")
EndFunc

Or

GUISetOnEvent($GUI_EVENT_CLOSE, "_FnStop")
GUICtrlSetOnEvent($giRun, "_FnRunPause")
GUICtrlSetOnEvent($giStart, "_FnStop")

No need to send your own GUI anything unless you're using keys (HotKey's would work for this), but for controls, the event functions should be enough, so you could essentially get rid of Start()/Stop() all together.

Edited by SmOke_N
damn code tags removed everything after first autoit code

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You're right about not needing to send keys to the GUI I don't know why i was doing that in the first place anymore.

I've replaced all the message boxes with consolewrite which works much better for debugging, however I still can't see why the script is getting stuck.

As far as I can see at the point it gets stuck the script is back to the initial state before RUN is clicked and it wasn't stuck initially.

I tried changing setting setting pause to false to calling the toggle pause function but either way the same behaviour occurs.

This is the current script I'm working with if anyone can shed some light on this issue it would be greatly appreciated.

#include <GUIConstantsEx.au3>                       ;For GUI
Global $Pause = False

HotKeySet("^r", "_FnRunPause")
HotKeySet("^t", "_FnStop")


;Create GUI
Opt("GUIOnEventMode", 1) ;Set on event mode for GUI
$hGUI = GUICreate("PS Mining Bot", 150, 250) ;Create main GUI window
$Run = GUICtrlCreateButton("RUN", 25, 25, 100, 75) ;Add Run button to GUI
$Stop = GUICtrlCreateButton("STOP", 25, 150, 100, 75) ;Add exit button to GUI
GUISetState(@SW_SHOW, $hGUI)

;Set actions on button press
GUISetOnEvent($GUI_EVENT_CLOSE, "_FnStop")
GUICtrlSetOnEvent($Run, "_FnRunPause")
GUICtrlSetOnEvent($Stop, "_FnStop")

_FnTogglePause()

;main code
While 1
    Sleep(1000)
    ConsoleWrite("... main loop..." & @CRLF)
WEnd

Func _FnStop()
    Exit
EndFunc

Func _FnRunPause()
    ConsoleWrite("... enter _FnRunPause()..." & @CRLF)
    Switch GUICtrlRead($Run)
        Case "RUN"
            GUICtrlSetData($Run, "PAUSE")
            ConsoleWrite("... going to toggle pause..." & @CRLF)
            _FnTogglePause()
        Case "PAUSE"
            GUICtrlSetData($Run, "RUN")
            ConsoleWrite("... going to toggle pause..." & @CRLF)
            _FnTogglePause()
    EndSwitch
EndFunc


Func _FnTogglePause()
     $Pause = Not $Pause
     ConsoleWrite("Enter Pause State: " & $Pause & @CRLF)
     While $Pause
         Sleep(1000)
         ConsoleWrite("... Pause loop..." & @CRLF)
         ConsoleWrite("...Pause state is:" & $Pause & @CRLF)
     WEnd
     ConsoleWrite("Exit Pause State: " & $Pause & @CRLF)
EndFunc

Regards

Share this post


Link to post
Share on other sites

cadjenk,

The GUI title "PS Mining Bot" caught my eye.

Can you please explain what this Bot is meant to do?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

It is a bot that automatically mines on a game. However this GUI will be useful for many different programs that are repetitive.

E.g. an incremental backup program that I'm wanting to use this GUI for.

Share this post


Link to post
Share on other sites

That's what I feared.

You seem to have missed to read the forum rules on your way in. A link to the rules can be found in the bottom right corner of each page.

You will learn that game automation of any kind is not permitted here :naughty:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I have read that I just didn't think this script would come under game automation. It's a multi-purpose GUI that can be used for lots of different things. I'm sorry if I have broken forum rules in this instance.

Share this post


Link to post
Share on other sites

We are quite strict here about any form of game automation because AutoIt had a bad reputation in the past when too many people used it to cheat on games.

Let's see what a Mod says.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

That's awesome, I feel like an ass.  I didn't even see the GUI title.  Glad everyone else was looking.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.