Jump to content
Sign in to follow this  
knight666

How do I exit a function with another click from a button?

Recommended Posts

knight666

Hi, I'm making a script that is looping indefinitely (while 1+1=2 teehee), and I was wondering, how do you exit it by clicking "Cancel"?

#include <GUIConstants.au3>

GUICreate("Template", 256, 256)

;here's some gui stuff going on, but that's not really important, is it?

While 1+1=2
$msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 If $msg = $btnCancel Then ExitLoop
 If $msg = $btnStart2 Then Go()
Wend

GUISetState(@SW_SHOW)

Func Go()
 While 1+1=2
  ;all you really need to know is that it's MASSIVE and that this doesn't work
  If GUIGetMsg() = $btnCancel Then ExitLoop
 WEnd

Func CLOSEClicked()
  Exit
EndFunc

I've stripped down the code (obviously) because I don't want my super awesome company secrets leaking out.

Thanks in advance.

Share this post


Link to post
Share on other sites
knight666

That uhm... is the exact same thing, that doesn't work.

Just because you have "While 1" instead of the totally more awesome "While 1+1=2" doesn't make it magically work.

But thanks for trying!

Share this post


Link to post
Share on other sites
knight666

The GUI shows up fine, thank you, otherwise this topic would be about that instead.

I simply made a mistake in my stripped down version.

Any more suggestions?

Share this post


Link to post
Share on other sites
Tripredacus

Before I even start, I did a Beta Run.

C:\Data\source\New Folder\close.au3(10,23) : WARNING: $btnCancel: possibly used before declaration.

If $msg = $btnCancel Then

~~~~~~~~~~~~~~~~~~~~~~^

C:\Data\source\New Folder\close.au3(11,23) : WARNING: $btnStart2: possibly used before declaration.

If $msg = $btnStart2 Then

~~~~~~~~~~~~~~~~~~~~~~^

C:\Data\source\New Folder\close.au3(22,1) : ERROR: syntax error

Func

^

C:\Data\source\New Folder\close.au3(10,23) : ERROR: $btnCancel: undeclared global variable.

If $msg = $btnCancel Then

~~~~~~~~~~~~~~~~~~~~~~^

C:\Data\source\New Folder\close.au3 - 2 error(s), 2 warning(s)

This may help you out.

Share this post


Link to post
Share on other sites
knight666

Thanks for running the code before reading about the errors I made in my stripped down version.

That really helped me out.

Share this post


Link to post
Share on other sites
PsaltyDS

Thanks for running the code before reading about the errors I made in my stripped down version.

That really helped me out.

Ohh... sarcasm. That always brings the best out in people... :)

All seriousness aside. You problem is likely a simple logic bug, but you posted an non-working example script with lots of unintended logic bugs not related to the problem. Reduce your demo to the shortest, simplest working script that will show the symptoms and post it. It will only take five minutes or less to code and test before posting. And in my experience, you'll have that "DOH!" moment while writing the demo where you see what the problem was all along.

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Tripredacus

There seems to be parts missing.

Also there is no EndFunc for the first function. I can get it to run by adding 2 buttons and declaring the variables, but no GUI appears.

Share this post


Link to post
Share on other sites
knight666

#include <GUIConstants.au3>

GUICreate("Template", 256, 256)
 ;gui, right here
 $btnStart2=GuiCtrlCreateButton("Go!",  64,64,  128,64)
 GUICtrlSetFont(-1,10,400,-1,"Courier New") 
 $btnCancel=GuiCtrlCreateButton("Cancel",  64,128,  128,64)
 GUICtrlSetFont(-1,10,400,-1,"Courier New")
GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

While 1+1=2
$msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 If $msg = $btnCancel Then ExitLoop
 If $msg = $btnStart2 Then GoHarder()
Wend

Func GoHarder()
 While 1+1=2
  If GUIGetMsg() = $btnCancel Then ExitLoop
  ;while loop
 WEnd
EndFunc

Func CLOSEClicked()
  Exit
EndFunc
oÝ÷ Ù«­¢+Ù=¡ ¸¸¸ÍÉÍ´¸Q¡Ð±ÝåÌÉ¥¹ÌÑ¡ÍнÕÐ¥¸Á½Á±¸¸¸

So far, the suggestions I received were:

-"while 1+1=2" is COMPLETELY different from "while 1"

-I can't seem to be able to correct code

-I did a beta run on the code you posted without checking by eye to see if the code was right, and it had bugs

So you know, a little sarcasm is in place.

Share this post


Link to post
Share on other sites
Shevilie

Not when your not providing code that doesnt show what the problem is.. like bringing your computer to the shop and says the OS is chrashing... though I left the videocard and powersuply at home, but can you test it and say whats wrong...


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites
Shevilie

It looks like you are trying to combine OnEvent and the normal.. which you can't..

http://www.autoitscript.com/autoit3/docs/g...OnEventMode.htm

There should be some reading how you use OnEvent correctly

Which you left out in the first post and - so noone could have guessed that

Edited by Shevilie

Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites
PsaltyDS

Sheville called it! :D

Demo fixed to show a usage of OnEventMode:

#include <GUIConstants.au3>
Opt("GuiOnEventMode", 1)

GUICreate("Template", 256, 256)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
$btnStart2 = GUICtrlCreateButton("Go!", 64, 64, 128, 64)
GUICtrlSetFont(-1, 10, 400, -1, "Courier New")
GUICtrlSetOnEvent(-1, "_GoHarder")
$btnCancel = GUICtrlCreateButton("Cancel", 64, 128, 128, 64)
GUICtrlSetFont(-1, 10, 400, -1, "Courier New")
GUICtrlSetOnEvent(-1, "_GoHarder")
GUISetState(@SW_SHOW)

While 1
    Sleep(20)
WEnd

Func _GoHarder()
    ; Do something...
    Switch @GUI_CtrlId
        Case $btnStart2
            ToolTip("You clicked Go!", 100, 100, "Template")
        Case $btnCancel
            ToolTip("You clicked Cancel!", 100, 100, "Template")
            Sleep(1000)
            ToolTip("")
    EndSwitch
EndFunc   ;==>_GoHarder

Func _Close()
    Exit
EndFunc   ;==>_Close

To help you along to the "DOH!" moment... :)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
knight666

Look, okay, you're still not getting it.

In my code, when GoHarder() is executed, it goes into a while loop.

Check @GUI_CtrlId doesn't work while in the while loop.

I know, because I just checked.

#include <GUIConstants.au3>

GUICreate("Template", 256, 256)
 ;gui, right here
 $btnStart2=GuiCtrlCreateButton("Go!",  64,64,  128,64)
 GUICtrlSetFont(-1,10,400,-1,"Courier New")
 $btnCancel=GuiCtrlCreateButton("Cancel",  64,128,  128,64)
 GUICtrlSetFont(-1,10,400,-1,"Courier New")
GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

While 1+1=2
$msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 If $msg = $btnCancel Then ExitLoop
 If $msg = $btnStart2 Then GoHarder()
Wend

Func GoHarder()
 While 1+1=2
  If @GUI_CtrlId = $btnCancel Then ExitLoop ;<== not werking
  If GUIGetMsg() = $btnCancel Then ExitLoop
  ;while loop
 WEnd
EndFunc

Func CLOSEClicked()
  Exit
EndFunc

Share this post


Link to post
Share on other sites
xcal

After removing the @GUI_CtrlId* line, what you posted above works, exiting the while loop inside GoHarder(). Maybe I'm not getting it either?

*That macro is for an event type function, as the help file states.

Share this post


Link to post
Share on other sites
PsaltyDS

Look, okay, you're still not getting it.

In my code, when GoHarder() is executed, it goes into a while loop.

Check @GUI_CtrlId doesn't work while in the while loop.

I know, because I just checked.

You can't use any OnEventMode functions and GuiGetMsg() at the same time. Your script will either be in OnEventMode or message mode. You can't mix them.

This version uses only message mode:

#include <GUIConstants.au3>

$RunFlag = False
GUICreate("Template", 256, 256)
$btnStart2 = GUICtrlCreateButton("Go!", 64, 64, 128, 64)
GUICtrlSetFont(-1, 10, 400, -1, "Courier New")
$btnCancel = GUICtrlCreateButton("Cancel", 64, 128, 128, 64)
GUICtrlSetFont(-1, 10, 400, -1, "Courier New")
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnStart2
            $RunFlag = True
            $Timer = TimerInit()
            ToolTip("You clicked Go!", 100, 100, "Template")
            Sleep(1000)
        Case $btnCancel
            $RunFlag = False
            ToolTip("You clicked Cancel!", 100, 100, "Template")
            Sleep(1000)
            ToolTip("")
    EndSwitch
    If $RunFlag Then $RunTime = ToolTip("Running... " & Round(TimerDiff($Timer) / 1000, 1) & " seconds", 100, 100, "Template")
WEnd

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
knight666

Yes, let's rehash the code I wrote previously because that worked so well.

Okay, you know what, here's my current code.

#include <GUIConstants.au3>

$gui=GUICreate("Template", 256, 256)

$tab=GUICtrlCreateTab (0,0, 256,256)

$tab1=GUICtrlCreateTabitem ( "Start")
GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $btnStart2=GuiCtrlCreateButton("Go!",  64,64,  128,64)
  GUICtrlSetFont(-1,10,400,-1,"Courier New") 
  $btnCancel=GuiCtrlCreateButton("Cancel",  64,128,  128,64)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")
$tab2=GUICtrlCreateTabitem ( "Blacklist")
GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $txtBlacklist=GUICtrlCreateEdit("", 5,29, 246,200)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")
  
  If FileExists("blacklist.txt")=0 Then 
   $file=FileOpen("blacklist.txt",2)
  Else 
   $file=FileOpen("blacklist.txt",0)
  EndIf
  
  $data = ""
  While 1+1=2
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   $data &= $line & @CRLF
  Wend
  GUICtrlSetData(-1,$data)
  
  $btnUpdate=GuiCtrlCreateButton("Update",  0,230,  128,24)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")  
  $btnRestore=GuiCtrlCreateButton("Restore",  128,230,  128,24)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")  
$tab3=GUICtrlCreateTabitem ( "Options")
GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $txtSec=GUICtrlCreateLabel ("Delay:", 10,40, 100,24)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $cmbSec=GUICtrlCreateCombo ("05 seconds", 100,40, 150,24, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $WS_VSCROLL))
  GUICtrlSetData(-1,"15 seconds|30 seconds|40 seconds|50 seconds|60 seconds|90 seconds|120 seconds|150 seconds|","30 seconds")
  GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $txtSec=GUICtrlCreateLabel ("Sitelist:", 10,64, 100,24)
  GUICtrlSetFont(-1,10,400,-1,"Courier New")

  $cmbFiles=GUICtrlCreateCombo ("<don't use sitelist>", 100,64, 150,24, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $WS_VSCROLL))
  GUICtrlSetFont(-1,10,400,-1,"Courier New")
  $search = FileFindFirstFile("list_*.txt")
  $data = ""
  If $search = -1 Then 
   $open=FileOpen("list_sites.txt",2)
   FileClose($open)
   $search = FileFindFirstFile("list_*.txt")
  EndIf
  $file2 = ""
  While 1+1=2
   $file = FileFindNextFile($search)
   If NOT $file = "" Then $file2 = $file
   If @error Then ExitLoop
   $data &= $file & "|"
  WEnd
   GUICtrlSetData(-1,$data,$file2)
   $chkRandomOrder=GUICtrlCreateCheckbox ("Randomize list order", 10, 88, 230, 24)
   GUICtrlSetFont(-1,10,400,-1,"Courier New")
   $chkLoopage=GUICtrlCreateCheckbox ("Put on eternal loop", 10, 112, 230, 24)
   GUICtrlSetFont(-1,10,400,-1,"Courier New")

GUICtrlCreateTabitem ("")

Opt("GUIOnEventMode", 1)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($btnStart2, "GoHarder")
GUICtrlSetOnEvent($btnCancel, "Cancel")
GUICtrlSetOnEvent($btnUpdate, "UpdateBlacklist")
GUICtrlSetOnEvent($btnRestore, "RestoreBlacklist")

GUISetState(@SW_SHOW)

While 1+1=2
;$msg = GUIGetMsg()
; If $msg = $GUI_EVENT_CLOSE Then ExitLoop
; If $msg = $btnCancel Then ExitLoop
; If $msg = $btnUpdate Then UpdateBlacklist()
; If $msg = $btnRestore Then RestoreBlacklist()
; If $msg = $btnStart2 Then GoHarder()
; ;If $msg = $btnAbout Then MsgBox(0,"about:","Created by knight666."&@CRLF&"Copyright (c) Knight666 Productions 2007.")
Wend

Func UpdateBlacklist()
 $file=FileOpen("blacklist.txt",2)
 FileWrite($file,GUICtrlRead($txtBlacklist))
 FileClose($file)
 MsgBox(64,"Ad-Cliken-4-U 1.00","Blacklist updated.")
EndFunc

Func RestoreBlacklist()
If MsgBox(49,"Ad-Cliken-4-U 1.00","This will undo all changes you have made to the file." & @CRLF & 'Are you sure?')=1 Then
  If FileExists("blacklist.txt")=0 Then 
   $file=FileOpen("blacklist.txt",2)
  Else 
   $file=FileOpen("blacklist.txt",0)
  EndIf
  
  $data = ""
  While 1+1=2
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
   $data &= $line & @CRLF
  Wend
  GUICtrlSetData($txtBlacklist,"")
  GUICtrlSetData($txtBlacklist,$data)
EndIf
EndFunc









Func GoHarder()
  $delay = Number(GUICtrlRead($cmbSec))*1000
  $starttime = @HOUR & ":" & @MIN & ":" & @SEC
  $retry=0

$boe = 1
Dim $blacklist[5000]
$file = FileOpen("blacklist.txt", 0)
While 1
 $line = FileReadLine($file)
 If @error = -1 Then ExitLoop
 $blacklist[$boe] = $line
 $boe += 1 
Wend

MouseClick("right", 5,155)
;WinWaitActive("Firefox","",30)
Dim $sitelist[5000]
$sitenumber=1
Sleep(1000)

;if GUICtrlRead($chkSitelist)=$GUI_UNCHECKED Then
If GUICtrlRead($cmbFiles)="<don't use sitelist>" Then
 Send ("!d^a^c")
 $sitelist[1]=ClipGet()
 $x=2
Else
 $x = 1
 ;$file = FileOpen("sitesptc.txt", 0)
 $file = FileOpen(GUICtrlRead($cmbFiles), 0)
 While 1+1=2
  $line = FileReadLine($file)
  If @error = -1 Then ExitLoop
  $sitelist[$x] = $line
  $x += 1 
 Wend
 Send("!d" & $sitelist[1] & "{ENTER}")
EndIf

Sleep(10000)
 
 While 1+1=2
  If @GUI_CtrlId = $btnCancel Then ExitLoop
  If $sitenumber=$x Then
   If GUICtrlRead($chkLoopage)=$GUI_UNCHECKED Then 
    ExitLoop
   Else
    $sitenumber=1
   EndIf
  EndIf
  
  Opt("WinTitleMatchMode", 4)
  If WinActive("classname=#32770") Then Send("{ESC}")
  Opt("WinTitleMatchMode", 1)
  
  ;MouseClick("right", 5,200, 1)
  ;sleep(10)
  ;Send("B")
  Send("^u")
  $active=WinWaitActive("Bron van: ","",30)
  
  If $active Then
   $retryforeel=0
   Sleep(1000)
   Send("^a")
   Sleep(1000)
   Send("^c")
   $html=ClipGet()
  
   $html2=$html
   $link=StringInStr($html2, "var url='")
   $html2=StringTrimLeft($html2,($link+8))
   $link2=StringInStr($html2, "'")
   $url=StringLeft($html2, (($link2)-1))
   ;MsgBox(0,"Sup.","URL: " & $url)
  
   $b = 1
   Dim $thisisalink[100]
   ;$message="Sup: "
   $reallinks=0
   While 1+1=2
    $str=StringInStr ($html, "runner.php")
    If $str = 0 Then ExitLoop
    $link=StringMid($html, $str, 20)
    $checklink=Number(StringMid($html, $str+14, 20))
    $html=StringTrimLeft($html,$str)
    
    $i = 1
    $totalexit=0
    While $i < $boe
     If ($url & $checklink) == $blacklist[$i] Then 
      $totalexit=1
      ExitLoop
     EndIf
    $i+=1
    WEnd
    
    ;NOT $checklink=0 AND NOT $checklink=1 
    If $checklink>1 AND NOT StringInStr($link,"REDIR") AND StringInStr($link,"?BA=")=0 AND $totalexit=0 Then
     $thisisalink[$b]=$checklink
     $reallinks=1
    EndIf
    $b+=1
   WEnd
  Else
   $retryforeel+=1
   If $retryforeel>3 Then
    Send("!d" & $sitelist[$sitenumber] & "{ENTER}")
    Sleep($delay)
    ExitLoop
   EndIf
  EndIf
  
  If WinActive("Bron van: ") Then
   Send("!{F4}")
   Sleep(1000)
  EndIf
  
  ;MsgBox(0,"Sup.","Reallinks: " & $reallinks & @CRLF & "Retry: " & $retry)

  If $b > 1 AND $url AND StringLen($url)<250 AND $reallinks=1 Then
   $retry=0
   
   $c = 0
   While 1+1=2
    $c+=1
    If $c > $b Then 
     ;$sitenumber+=1
     Send("!d" & $sitelist[$sitenumber] & "{ENTER}")
     Sleep($delay)
     ExitLoop
    EndIf
    If NOT $thisisalink[$c]=0 Then
     Send("!d" & $url & $thisisalink[$c] & "{ENTER}")
     Sleep($delay)
    EndIf
   WEnd
  Else
   $retry+=1
   If $retry>2 Then
    $sitenumber+=1
    $retry=0
    Send("!d" & $sitelist[$sitenumber] & "{ENTER}")
    Sleep($delay)
   EndIf
  EndIf
 WEnd
MsgBox(0,"A crash occurred!","It appears there are no more PTC links to click!" & @CRLF & "" & @CRLF & "Starttime: " & $starttime & @CRLF & "Crashtime: " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF)
EndFunc

Func Cancel()
  MsgBox(0,"Ad-Cliken-4-U 1.00","Sequence cancelled.")
EndFunc

Func CLOSEClicked()
  Exit
EndFunc

:)

Man it must be because I'm such a dumb noob that I can't seem to get it working.

Share this post


Link to post
Share on other sites
erebus

I am curious... Can you explain to us how the While 1 is different from While 1+1=2 ?

While only checks if the statement after the command is true. So:

While $myname = "blah"

While 1

While 1+1 = 2

While 8+8 = 16

and any other TRUE statement has EXACTLY the same result.

Now, before insisting for something without proving it, enlight us what is the difference in your case.

Share this post


Link to post
Share on other sites
knight666

What I've been trying to explain is that there is NO DIFFERENCE.

It is merely another way to make an eternal loop.

Yet someone made the suggestion I change it.

It originates from my days making games for my calculator, which went something like this:

"Well I need to check for keystrokes all the time, how do I do that?

Uhm... 1+1 is always 2..."

Share this post


Link to post
Share on other sites
erebus

So far, the suggestions I received were:

-"while 1+1=2" is COMPLETELY different from "while 1"

That was NOT my suggestion on the first post. My suggestion was to add this:

$msg = GUIGetMsg()

That simple.

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
Sign in to follow this  

×