Jump to content

Ending Function button


Kapz
 Share

Recommended Posts

I have a simple GUI setup so that when a particular button is pressed, a function is called which proceeds in an infinite loop. This is fine, however, when I want to manually stop the loop by pressing another button, the loop ignores the button press (on the GUI) and continues to run. I tried to stop the function by calling on another function which uses the Exit command. Is there any way to accomplish this?

Link to comment
Share on other sites

I have a simple GUI setup so that when a particular button is pressed, a function is called which proceeds in an infinite loop. This is fine, however, when I want to manually stop the loop by pressing another button, the loop ignores the button press (on the GUI) and continues to run. I tried to stop the function by calling on another function which uses the Exit command. Is there any way to accomplish this?

Would be easier to help if you posted your code, but it sounds to me like the function you are caling has a loop itself, that prevent the main loop from getting the button pres, try post the func or the whole program.

Link to comment
Share on other sites

Would be easier to help if you posted your code, but it sounds to me like the function you are caling has a loop itself, that prevent the main loop from getting the button pres, try post the func or the whole program.

Sorry if I posted too much code, but I want to be sure that I'm approaching this correctly. When I click on the 'Go' button, the function starts running its loop and wont stop unless I physically press the 'Esc' key, but I need it to stop when I press the "Stop" button which calls the MyExit function. Is there a way to break the loop when this button is pressed? Also, I was wondering if there might be a way to send information such as the current value of the $x variable to the GUI while the for loop is going, say for each individual iteration.

#include <IE.au3>

#include <GuiConstants.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{ESC}", "MyExit")

$x = 0

; GUI

GuiCreate("Test", 200, 350)

Opt("GUIOnEventMode",1)

GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")

$var = RegEnumVal("HKEY_LOCAL_MACHINE\SOFTWARE\Test", 2)

$var2 = RegEnumVal("HKEY_LOCAL_MACHINE\SOFTWARE\Test", 3)

$oldemail = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Test",$var)

$oldpasswd = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Test",$var2)

GuiCtrlCreateLabel("Your Login Information:", 10, 20)

GuiCtrlCreateLabel("Email: " & $oldemail, 45, 48)

GuiCtrlCreateLabel("Password: " & $oldpasswd, 24, 70)

$new = GUICtrlCreateButton("Change Login", 58, 100, 100, 30)

GUISetOnEvent($GUI_EVENT_CLOSE,"Addnew")

$new2 = GuiCtrlCreateButton("Go!", 58, 175, 100, 30)

GUISetOnEvent($GUI_EVENT_CLOSE,"Edit")

$new3 = GuiCtrlCreateButton("Stop", 58, 225, 100, 30)

GUISetOnEvent($GUI_EVENT_CLOSE,"MyExit")

GUISetState ()

; GUI MESSAGE LOOP

GuiSetState()

While GuiGetMsg() <> $GUI_EVENT_CLOSE

WEnd

Func Addnew()

$email = InputBox("Email","Please enter your email address")

$passwd = InputBox("Password","Please enter your password")

$rw = RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Test","Email","REG_SZ",$email)

$rw = RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Test","Password","REG_SZ",$passwd)

EndFunc

Func Edit()

$x = 0

$oIE = _IECreate()

_IENavigate ($oIE, "http://www.autoitscript.com")

$o_form = _IEFormGetObjByName($oIE, "theForm")

$o_login = _IEFormElementGetObjByName($o_form, "email")

$o_password = _IEFormElementGetObjByName($o_form, "password")

_IEFormElementSetValue($o_login, $email)

_IEFormElementSetValue($o_password, $passwd)

_IEFormSubmit($o_form)

_IELoadWait($oIE)

_IEClickLinkByText ($oIE, "Forum")

_IELoadWait($oIE)

$begin = TimerInit()

For $x = 1 to 1000000

$temp = _IEFormGetCount($OIE)

If $temp = 3 Then

$oFrom = _IEFormGetObjByIndex($oIE, 2)

$change = Random(4, 13, 1)

$oRelevant = _IEFormElementGetObjByIndex($oFrom, $change)

$oRelevant.Click

_IELoadWait($oIE)

Else

_IENavigate ($oIE, "http://www.autoitscript.com")

$o_form = _IEFormGetObjByName($oIE, "theForm")

$o_login = _IEFormElementGetObjByName($o_form, "email")

$o_password = _IEFormElementGetObjByName($o_form, "password")

;----------Set field values and submit the form

_IEFormElementSetValue($o_login, $email)

_IEFormElementSetValue($o_password, $passwd)

_IEFormSubmit($o_form)

_IELoadWait($oIE)

_IEClickLinkByText ($oIE, "Forum")

_IELoadWait($oIE)

EndIf

Next

EndFunc

Func MyExit()

$diff = TimerDiff($begin)

MsgBox(0,"Test","You have seen " & $x & " topics in " & int($diff/60000) & " minutes.")

Exit

EndFunc

Func OnExit()

Exit

EndFunc

Link to comment
Share on other sites

If this is the whole code, i can't see why you would use "Opt("GUIOnEventMode", 1)" but the reason your program is not closing is, that when pushing the Go button, the program enters the For.. Next Loop in your Edit function. You are not even using the For..Next loop for what it is sopossed to do ;) Since i don't have the IE.aue, i can't test it, but i will post a correction in a minut

Link to comment
Share on other sites

Thank you so much for your help. I was able to get the GUI to work properly but when I clicked on the "Stop" button, or even the 'X' at the top right of the GUI while the Edit function was running, nothing would happen. For some reason, manually typing the 'Esc' key ended the entire program and seemed to work fine during the While loop (previously the For loop). Any ideas?

Link to comment
Share on other sites

Yeah, so basically the modified program that you created (prog.au3) still does not exit the function even if the while loop is used once I click on one of the buttons in on the GUI while the external while loop is running.

I tried using the "Opt("GUIOnEventMode", 1)" command at the beginning of the program but once I did, all of the buttons were completely non-responsive.

Link to comment
Share on other sites

Yeah, so basically the modified program that you created (prog.au3) still does not exit the function even if the while loop is used once I click on one of the buttons in on the GUI while the external while loop is running.

I tried using the "Opt("GUIOnEventMode", 1)" command at the beginning of the program but once I did, all of the buttons were completely non-responsive.

Im a little confused. When you click the GO button, the program shoud enter the Edit() func that does something... and while the program is looping in the new loop in the Edit() func, you should be able to press the Stop button, which executes the MyExit() func right?

Link to comment
Share on other sites

Im a little confused. When you click the GO button, the program shoud enter the Edit() func that does something... and while the program is looping in the new loop in the Edit() func, you should be able to press the Stop button, which executes the MyExit() func right?

That is correct, however, the Stop button does nothing, neither does the close window 'X' at the top right corner after I click the GO button and the program enters the Edit() function. However, before clicking on the GO button, the Stop button works just fine. Should I be using the "Opt("GUIOnEventMode", 1)" command somewhere?

Link to comment
Share on other sites

That is correct, however, the Stop button does nothing, neither does the close window 'X' at the top right corner after I click the GO button and the program enters the Edit() function. However, before clicking on the GO button, the Stop button works just fine. Should I be using the "Opt("GUIOnEventMode", 1)" command somewhere?

Hmm thats odd, it works just fine here, are you using the latest beta?

Prog1.au3

This works perfecly here...

Link to comment
Share on other sites

I found out what was causing the problem, the command _IELoadWait($oIE) from the IE.au3 waits for a webpage to fully load before going on to the next step, no matter what I press and then after it finishes loading, it goes on the next step. Once I removed it, the Stop button worked fine but my script wasn't working since it needs to wait for the page to fully load before it can continue. I tried placing

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

If $msg = $new3 Then ExitLoop

right before and even right after the _IELoadWait($oIE) command, so that when I clicked on the Stop button a hundred times after a few seconds it finally realized it had ended the loop. Is there any way to do this completely external from the while loop and the function, so that I don't have to check for the status of $msg at some point during the loop?

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