Sign in to follow this  
Followers 0
Dethredic

Stoping a Function

19 posts in this topic

I have a program that mainly runs a function.

I put a "Stop" button on the GUI so you could stop the function if you wanted to.

Is there any way to stop a function without exiting the script?


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites



Return..?

Share this post


Link to post
Share on other sites

Let's say the Stop button is called $StopButton.

Func MyFunc ($MyArg)
    While (1)
        MsgBox (0, "MyMsgBox", "I like digimon!")
        Sleep (1000)
        If GUIGetMsg () = $StopButton Then ExitLoop
    WEnd
    MsgBox (0, "MyMsgBox", "I still like digimon!")
EndFunc

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

I have a program that mainly runs a function.

I put a "Stop" button on the GUI so you could stop the function if you wanted to.

Is there any way to stop a function without exiting the script?

You've been here long enough to know that we can't give a whole lot of help without a code... :P

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You've been here long enough to know that we can't give a whole lot of help without a code... :P

Sorry i didn't know it depended on the code, i thought it would be like a EndFunc(start) likeish thing

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


$Form1 = GUICreate("Prime Generator", 625, 452, 266, 132)
$i_inputnumber = GUICtrlCreateInput("", 40, 160, 193, 21 )
$edit_results = GUICtrlCreateEdit("", 272, 8, 341, 425,$ES_READONLY + $ES_WANTRETURN + $WS_VSCROLL + $WS_HSCROLL + $ES_AUTOVSCROLL + $ES_AUTOHSCROLL)
GUICtrlSetBkColor ($edit_results,0xffffff)
GUICtrlCreateLabel("Prime Generator", 51, 16, 182, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("Prime Numbers To Generate To:", 40, 128, 196, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Generate = GUICtrlCreateButton("Generate Primes", 80, 200, 105, 33, 0)
$Stop = GUICtrlCreateButton("Stop", 80, 250, 105, 33, 0)
GUICtrlCreateLabel("By: Joe", 82, 400, 100, 20);;
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Generate,"Start")
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

While 1
    sleep(1000)
Wend
Exit
    
    
Func Start()
    GUICtrlSetData($edit_results,"")
    Local $i_number = Number(GUICtrlREad($i_inputnumber))
    GUICtrlSetData($i_inputnumber,$i_number)
    If $i_number < 2 Then
        MsgBox(64, "Error!", "You didn't enter a valid number.  Please try again.")
        Else
    CalculatePrimes(GUICtrlREad($i_inputnumber),True)
    EndIf
EndFunc

Func CalculatePrimes($i_PrimeToCalcTo, $comments = True)
    Opt("GUIOnEventMode",1)
    $counter = 1
    If $comments = True Then
        GUICtrlSetData($edit_results, 2 & @CRLF)
    Else
    GUICtrlSetData($edit_results,2 & @CRLF)
    EndIf
    For $Number = 3 To $i_PrimeToCalcTo Step 2
        $divisor = 3
        While 1
            If $divisor * $divisor > $Number Or Mod($Number, $divisor) = 0 Then ExitLoop
            $divisor = $divisor + 2
        WEnd
        If $divisor * $divisor > $Number Then  
            $counter = $counter + 1  
            If $comments = True Then
                GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
            Else
                GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
            EndIf
           
        EndIf
    Next
    GUICtrlSetData($edit_results, "There were " & $counter & " primes found between 2 and " & $i_PrimeToCalcTo & @CRLF, 1)
EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CTRLID = $GUI_EVENT_CLOSE
            Exit           
        Case @GUI_CTRLID = $GUI_EVENT_MINIMIZE
           
        Case @GUI_CTRLID = $GUI_EVENT_RESTORE
           
    EndSelect
   
EndFunc

I want the stop button to stop the part that calculating prime numbers

Edited by Dethredic

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

just check for the button press inside that loop...........


The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Just paste this in your code:

Func CalculatePrimes($i_PrimeToCalcTo, $comments = True)
     Opt("GUIOnEventMode",0)
     $counter = 1
     If $comments = True Then
         GUICtrlSetData($edit_results, 2 & @CRLF)
     Else
     GUICtrlSetData($edit_results,2 & @CRLF)
     EndIf
     For $Number = 3 To $i_PrimeToCalcTo Step 2
         If GUIGetMsg() = $stop Then
             Opt("GUIOnEventMode",1)
             Return
         EndIf
         $divisor = 3
         While 1
             If $divisor * $divisor > $Number Or Mod($Number, $divisor) = 0 Then ExitLoop
             $divisor = $divisor + 2
         WEnd
         If $divisor * $divisor > $Number Then  
             $counter = $counter + 1  
             If $comments = True Then
                 GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
             Else
                 GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
             EndIf
           
         EndIf
     Next
     GUICtrlSetData($edit_results, "There were " & $counter & " primes found between 2 and " & $i_PrimeToCalcTo & @CRLF, 1)
 EndFunc
Edited by sheckandar

Live and Learn, 'cause Knowledge is Super Power.

Share this post


Link to post
Share on other sites

@sheckandar

Thanks, it works, but it is about 100x slower now :P (i guess that was to be expected by adding another check into the function)


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

Try using a global flag variable.

Instead of checking for GUI messages in the function, just check the value of the flag.

Then enable an Adlib function every X milliseconds to check for GUI messages. The Adlib function would change the flags's value.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Sorry, but can you give me an example?


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

Sorry, but can you give me an example?


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

Global $StopFlag

Func MyHappyStop ()
    If GUIGetMsg () = $StopButton Then
        $StopFlag = True
    EndIf
EndFunc

$GUI = GUICreate ()
$StopButton = GUICtrlCreateButton ("Stop", 0, 0, 60, 20)
GUISetState ()

AdlibEnable ("MyHappyStop", 500)

$StopFlag = False
While (1)
    For &i = 1 To 99999 Step 1
        ;Doing something boring here
        If $StopFlag Then ExitLoop
    Next
WEnd


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

Was posting like 5 times really necessary?

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Yep :P

Well....

No :)

I actually that wasn't ment for me i don't think

Nope not me :D

Edited by Dethredic

"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

The server were off. When I clicked Add Reply it just said Service down ou temporarily unavailabe.

Sorry for the 5 posts. I'll delete them.


[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

@CoePSX

I couldn't get your code to work

i put the main part of my script in the "do something boring here" part

could you please add it to my code that i posted earlier


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

#include <GUIConstants.au3>
Opt("GUIOnEventMode",1)
Global $_StopFlag

$Form1 = GUICreate("Prime Generator", 625, 452, 266, 132)
$i_inputnumber = GUICtrlCreateInput("", 40, 160, 193, 21 )
$edit_results = GUICtrlCreateEdit("", 272, 8, 341, 425,$ES_READONLY + $ES_WANTRETURN + $WS_VSCROLL + $WS_HSCROLL + $ES_AUTOVSCROLL + $ES_AUTOHSCROLL)
GUICtrlSetBkColor ($edit_results,0xffffff)
GUICtrlCreateLabel("Prime Generator", 51, 16, 182, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("Prime Numbers To Generate To:", 40, 128, 196, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Generate = GUICtrlCreateButton("Generate Primes", 80, 200, 105, 33, 0)
$Stop = GUICtrlCreateButton("Stop", 80, 250, 105, 33, 0)
GUICtrlCreateLabel("By: Mr. X", 82, 400, 100, 20) ;;
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Generate,"Start")
GUICtrlSetOnEvent($Stop,"Stop")
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

While 1
        sleep(1000)
Wend
Exit
  
   Func Start()
        GUICtrlSetData($edit_results,"")
        Local $i_number = Number(GUICtrlREad($i_inputnumber))
        GUICtrlSetData($i_inputnumber,$i_number)
        If $i_number < 2 Then
                MsgBox(64, "Error!", "You didn't enter a valid number.  Please try again.")
            Else
            CalculatePrimes(GUICtrlREad($i_inputnumber),True)
        EndIf
EndFunc

Func Stop ()
    $_StopFlag = True
EndFunc

Func CalculatePrimes($i_PrimeToCalcTo, $comments = True)
        Opt("GUIOnEventMode",1)
    $_StopFlag = False
        $counter = 1
        If $comments = True Then
                GUICtrlSetData($edit_results, 2 & @CRLF)
        Else
            GUICtrlSetData($edit_results,2 & @CRLF)
        EndIf
        For $Number = 3 To $i_PrimeToCalcTo Step 2
                $divisor = 3
                While 1
            If $_StopFlag Then
                GUICtrlSetData($edit_results, "Function stoped.", 1)
                Return
            EndIf
                        If $divisor * $divisor > $Number Or Mod($Number, $divisor) = 0 Then ExitLoop
                        $divisor = $divisor + 2
                WEnd
                If $divisor * $divisor > $Number Then 
                         $counter = $counter + 1 
                         If $comments = True Then
                                GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
                        Else
                                GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
                        EndIf
         EndIf
        Next
        GUICtrlSetData($edit_results, "There were " & $counter & " primes found between 2 and " & $i_PrimeToCalcTo & @CRLF, 1)
EndFunc

Func SpecialEvents()
        Select
                Case @GUI_CTRLID = $GUI_EVENT_CLOSE
                        Exit 
                          Case @GUI_CTRLID = $GUI_EVENT_MINIMIZE
 
                          Case @GUI_CTRLID = $GUI_EVENT_RESTORE
     EndSelect
 EndFunc

EDIT: O__o How the hell did this snippet got that way?

Edited by CoePSX

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Share this post


Link to post
Share on other sites

This should work:

#include <GUIConstants.au3>
Opt("GUIOnEventMode",1)
;~ #include <Prime.au3>

Global $ISCalculating = False
Global $IsPaused = False
$Form1 = GUICreate("Prime Generator by The Kandie Man", 525, 452, 266, 132)
$i_inputnumber = GUICtrlCreateInput("", 40, 160, 193, 21 )
$edit_results = GUICtrlCreateEdit("", 272, 8, 241, 425,$ES_READONLY + $ES_WANTRETURN + $WS_VSCROLL + $WS_HSCROLL + $ES_AUTOVSCROLL + $ES_AUTOHSCROLL)
GUICtrlSetBkColor ($edit_results,0xffffff)
GUICtrlCreateLabel("Prime Generator", 56, 16, 182, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("Prime Numbers To Generate To", 40, 128, 196, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$Generate = GUICtrlCreateButton("Generate Primes", 80, 200, 105, 33, 0)
$Pause = GUICtrlCreateButton("Pause",80, 240, 105, 33, 0)
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Generate,"Start")
GUICtrlSetOnEvent($Pause ,"Pause")
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")


While 1
    sleep(100)
    If $ISCalculating = True Then
        GUICtrlSetData($edit_results,"")
        Local $i_number = Number(GUICtrlREad($i_inputnumber))
        GUICtrlSetData($i_inputnumber,$i_number)
        If GUICtrlREad($i_inputnumber) <> 0 Then
            CalculatePrimes(GUICtrlREad($i_inputnumber),True)
        Endif
        GUICtrlSetData($Generate,"Generate Primes")
    Endif
Wend
Exit

Func Start()
    If $ISCalculating = True Then
        $ISCalculating = False
        GUICtrlSetData($Generate,"Generate Primes")
    ElseIf $ISCalculating = False Then
        $ISCalculating = True
        GUICtrlSetData($Generate,"Stop")
    Endif
EndFunc
Func Pause()
    If $IsPaused = False Then
        $IsPaused = True
        GUICtrlSetData($Pause,"Resume")
    ElseIf $IsPaused = True Then
        $IsPaused = False
        GUICtrlSetData($Pause,"Pause")
    EndIf
    
EndFunc
Func CalculatePrimes($i_PrimeToCalcTo, $comments = True)
    Opt("GUIOnEventMode",1)
    $counter = 1
    If $comments = True Then
        GUICtrlSetData($edit_results,'Prime Number1: ' & 2 & @CRLF)
    Else
    GUICtrlSetData($edit_results,2 & @CRLF)
    EndIf
    Local $product
    For $Number = 3 To $i_PrimeToCalcTo Step 2
        If $ISCalculating = False Then Return 0
        If $IsPaused = False Then
            $divisor = 3
            While 1
                $product = $divisor * $divisor
                If $product > $Number Or Mod($Number, $divisor) = 0 Then ExitLoop
                $divisor = $divisor + 2
            WEnd
            If $product > $Number Then   ; are all divisor exhausted?
                $counter = $counter + 1 
                         ; yes, this Number is a prime
                If $comments = True Then
;~            ConsoleWrite('Prime number #' & $counter  & ': ' & $Number & @LF)
                    GUICtrlSetData($edit_results, "Prime Number:" & $counter & ":" & $Number & @CRLF & GUICtrlRead($edit_results))
;~           FileWrite($h_filehandle,'Prime number #' & $counter  & ': ' & $Number & @CRLF)
                Else
;~         ConsoleWrite($Number & @LF)
                    GUICtrlSetData($edit_results, $Number & @CRLF & GUICtrlRead($edit_results))
;~          FileWrite($h_filehandle,$Number & @CRLF)
                EndIf
            
            EndIf
        Else
            sleep(50)
        Endif
    Next
EndFunc   ;==>CalculatePrimes

Func SpecialEvents()
    Select
        Case @GUI_CTRLID = $GUI_EVENT_CLOSE
            Exit            
        Case @GUI_CTRLID = $GUI_EVENT_MINIMIZE
            
        Case @GUI_CTRLID = $GUI_EVENT_RESTORE
            
    EndSelect
    
EndFunc

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Perfect, Thanks The Kandie Man


"Its not about the 30 inch 1080p display, or the SLI 8800 ultras, or the DDR3 memory. It's about when you turn on your PC, does it return the favor?"Math is like sex. Sure, it may give some practical results, but that is not why we do it

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  
Followers 0