Jump to content

How to stop a do untill loop


Nova
 Share

Recommended Posts

I was asked today by a maths teacher, if I could write a program to find all numerical palendrome from 0 to any given number.

I said yea of cource piece of cake.......then I went a bit mad at gui detail and heres the final product.

I have one problem however

When I press $button1 Calculate ive made it so the button changes function into a stop button.

But I cannot get the stop button to stop the Do Untill loop which is at that moment trying to satisify the expression.

#include <GuiConstants.au3>
#Include <string.au3>
#NoTrayIcon
Opt("GUIOnEventMode",1)

GuiCreate("Cathal's Palendrome Calculator", 320, 380, -1, -1)
$Group1 = GUICtrlCreateGroup ("Palendrome Calc", 10, 20, 300, 100)
$Group2 = GUICtrlCreateGroup ("Results", 10, 130, 300, 240)
$Edit = GuiCtrlCreateEdit("", 40, 150, 240, 170, $WS_VSCROLL)
$Input = GuiCtrlCreateInput("", 120, 61, 125, 23)
$Button1 = GuiCtrlCreateButton("Calculate", 245, 60, 60, 25)
$Button2 = GuiCtrlCreateButton("Save results", 120, 330, 80, 25)
$Label = GuiCtrlCreateLabel("Cut off value (integer)", 15, 65, 104, 20)
GuiSetState()

GuiSetOnEvent($GUI_EVENT_CLOSE, "quit")
GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
GUICtrlSetOnEvent($Button2, "Save_Pressed")

Func Calculate_Pressed()
    $limit = GUICtrlRead( $Input )
    $chk = StringIsFloat($limit)    
        If $chk = 1 Then
            MsgBox(0,"Test","You must enter a positive integer")
        ElseIf $limit = 0 Then
            MsgBox(0,"Test","You must enter a positive integer")
        ElseIf $limit = 1 Then
            GUICtrlSetData($Edit, "Occurance = 1        Palendrome = 1", 1)
            GUICtrlSetData($Edit, @CRLF, 1)
        ElseIf $limit > 1 Then
            GUICtrlSetState ( $Edit, $GUI_DISABLE )
            GUICtrlSetData($Edit, "")
            GUICtrlSetData ( $Button1, "Stop" )
            GUICtrlSetOnEvent($Button1, "Stop_Pressed")
            $string = 1
            $i = 1 
                Do
                    $stringrev = _StringReverse($string)
                        If $string = $stringrev Then
                            sleep(250)
                            GUICtrlSetData($Edit, "Occurance = " & $i & "      Palendrome = " & $string, 1)
                            GUICtrlSetData($Edit, @CRLF, 1)
                            $i = $i + 1     
                            $string = $string + 1
                        Else
                            $string = $string + 1
                        EndIf
                Until $string = $limit 
            
                    $limitrev = _StringReverse($limit)
                        If $limit = $limitrev Then
                            GUICtrlSetData($Edit, "Occurance = " & $i & "      Palendrome = " & $limit, 1)
                        Else
                        EndIf 
            GUICtrlSetState ( $Edit, $GUI_ENABLE )
            GUICtrlSetData ( $Button1, "Calculate" )
            GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
        EndIf
EndFunc

Func Stop_Pressed ()
    GUICtrlSetData ( $Button1, "Calculate" )
    GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
EndFunc

Func Save_Pressed ()
    $string = GUICtrlRead($edit)
    $stringlen = StringLen($string)
    If $stringlen = 0 Then
        MsgBox(0,"Error","No data to save")
    Else
        FileWriteLine(@ScriptDir & "\Results.txt", $string )
    EndIf       
EndFunc

Func quit ()
    Exit
EndFunc

While 1
    sleep(10)
WEnd

By the way for anyone that dosent know, a numerical palendrome is a number like 121 which can be reversed and it still has the same value.

Link to comment
Share on other sites

AutoIt3 is mono Thread so if you are executing a script line no other line can take place.

The only possibility is to put your code in an adlib function and to take care of action done by the GUI. You have to limit what your adlib function does unless it will also hog the CPU.

difficult stuff to do in a mono thread even in ADLIB

Link to comment
Share on other sites

  • Developers

think this way works:

#include <GuiConstants.au3>
#Include <string.au3>
#NoTrayIcon
Opt ("GUIOnEventMode", 1)

GUICreate("Cathal's Palendrome Calculator", 320, 380, -1, -1)
$Group1 = GUICtrlCreateGroup("Palendrome Calc", 10, 20, 300, 100)
$Group2 = GUICtrlCreateGroup("Results", 10, 130, 300, 240)
$Edit = GUICtrlCreateEdit("", 40, 150, 240, 170, $WS_VSCROLL)
$Input = GUICtrlCreateInput("", 120, 61, 125, 23)
$Button1 = GUICtrlCreateButton("Calculate", 245, 60, 60, 25)
$Button2 = GUICtrlCreateButton("Save results", 120, 330, 80, 25)
$Label = GUICtrlCreateLabel("Cut off value (integer)", 15, 65, 104, 20)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "quit")
GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
GUICtrlSetOnEvent($Button2, "Save_Pressed")

Func Calculate_Pressed()
    $limit = GUICtrlRead($Input)
    $chk = StringIsFloat($limit)
    If $chk = 1 Then
        MsgBox(0, "Test", "You must enter a positive integer")
    ElseIf $limit = 0 Then
        MsgBox(0, "Test", "You must enter a positive integer")
    ElseIf $limit = 1 Then
        GUICtrlSetData($Edit, "Occurance = 1        Palendrome = 1", 1)
        GUICtrlSetData($Edit, @CRLF, 1)
    ElseIf $limit > 1 Then
        GUICtrlSetState($Edit, $GUI_DISABLE)
        GUICtrlSetData($Edit, "")
        GUICtrlSetData($Button1, "Stop")
        Opt ("GUIOnEventMode", 0)
    ;GUICtrlSetOnEvent($Button1, "Stop_Pressed")
        $string = 1
        $i = 1
        Do
            $msg = GUIGetMsg()
            If $msg = $Button1 then ExitLoop
            $stringrev = _StringReverse($string)
            If $string = $stringrev Then
                Sleep(250)
                GUICtrlSetData($Edit, "Occurance = " & $i & "      Palendrome = " & $string, 1)
                GUICtrlSetData($Edit, @CRLF, 1)
                $i = $i + 1
                $string = $string + 1
            Else
                $string = $string + 1
            EndIf
        Until $string = $limit
        Opt ("GUIOnEventMode", 1)
        
        $limitrev = _StringReverse($limit)
        If $limit = $limitrev Then
            GUICtrlSetData($Edit, "Occurance = " & $i & "      Palendrome = " & $limit, 1)
        Else
        EndIf
        GUICtrlSetState($Edit, $GUI_ENABLE)
        GUICtrlSetData($Button1, "Calculate")
        GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
    EndIf
EndFunc  ;==>Calculate_Pressed

Func Stop_Pressed()
MsgBox(262144,'debug line ~61' , 'Stop_Pressed():' );### Debug MSGBOX
    GUICtrlSetData($Button1, "Calculate")
    GUICtrlSetOnEvent($Button1, "Calculate_Pressed")
EndFunc  ;==>Stop_Pressed 

Func Save_Pressed()
MsgBox(262144,'debug line ~67' , 'Save_Pressed():');### Debug MSGBOX
    $string = GUICtrlRead($Edit)
    $stringlen = StringLen($string)
    If $stringlen = 0 Then
        MsgBox(0, "Error", "No data to save")
    Else
        FileWriteLine(@ScriptDir & "\Results.txt", $string)
    EndIf
EndFunc  ;==>Save_Pressed 

Func quit()
    Exit
EndFunc  ;==>quit 

While 1
    Sleep(10)
WEnd

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Didnt work for me, the script only stopped when it was finished its untill loop.

Enter 9999 into the input and try stopping the loop with the stop button.

<{POST_SNAPBACK}>

works for me.... did you Cut&Paste the whole code ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Yes it is infact working now, I must have copyed wrong.

Tnx JdeB

Edit

MsgBox(262144,'debug line ~61' , 'Stop_Pressed():' );### Debug MSGBOX

Whats the debug msgbox do ? Edited by Nova
Link to comment
Share on other sites

I think the problem is that the stop button can be pressed at any time but most of the time the loop is in the sleep(250) delay so doesn't see it.

If you replace the sleep with a little loop then I think it will work.

Here is how I think you should modify your do until loop.

Do

$msg = GUIGetMsg()

If $msg = $Button1 then ExitLoop

If $string = number(_StringReverse($string)) Then

;Sleep(250) don't do this

$starttime = Timerinit();

$getout = 0;

do

If GUIGetMsg() = $Button1 then

$getout = 1

ExitLoop

endif

until TimerDiff($starttime) > 250

if $getout then exitloop

GUICtrlSetData($Edit, "Occurance = " & $i & " Palendrome = " & $string, 1)

GUICtrlSetData($Edit, @CRLF, 1)

$i = $i + 1

EndIf

$string = $string + 1

Until $string = $limit

Opt ("GUIOnEventMode", 1)

if $string = $limit then

$limitrev = _StringReverse($limit)

If $limit = $limitrev Then

GUICtrlSetData($Edit, "Occurance = " & $i & " Palendrome = " & $limit, 1)

EndIf

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Developers

Yes it is infact working now, I must have copyed wrong.

Tnx JdeB

Edit

Whats the debug msgbox do ?

<{POST_SNAPBACK}>

I use SciTE (Obviously) .. so put a couple of debug messages in with Ctrl+Shift+D to see what to code was doing...... that whole function is not used anymore i guess ...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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