Jump to content

Problem with loop ! Need help ...


Recommended Posts

Hi all. I'm newbie. With many try, i have the problems with loop such as For ... to ... next, while ... wend, ... I found that when a loop is doing, everything (such as gui even, control even, others func() ...) must be waitting until it finished to work. With a long time loop, everything will continue after a long time it finished. I wonder why there is a first loop (While 1 | Sleep(1000) | WEnd) to keep the GUI always on, everything can work simultaneously with it. But when have the second loop is running, everything can't work at the same time with the second. How can i solve this problems.
Tks in advance.

Link to comment
Share on other sites

  • Moderators

hiepkhachsg,

You should only need a Sleep(10) in your idle loop to keep the script alive - but quite what else is going wrong is impossible to say without seeing the script itself. So I suggest you post it - see here how to do it - so we can take a look.

M23

 

 

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

As you can see in this script:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$gui = GUICreate("KLeines 1 X 1", 400, 120)
$Start = GUICtrlCreateButton("&Start", 10, 10)
$PauseResume = GUICtrlCreateButton("&Pause", 10, 40)
$Anzeige = GUICtrlCreateLabel("Anzeige", 10, 90, 380)
GUISetState()

While 1
    _EventHandler()
WEnd

Func _EventHandler()
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Start
            GUICtrlSetState($Start, $GUI_DISABLE)
            For $i = 1 To 10
                For $j = 1 To 10
                    GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
                    _MySleep(500)
                Next
            Next
            GUICtrlSetState($Start, $GUI_ENABLE)
        Case $PauseResume
            If GUICtrlRead($PauseResume) = "&Pause" Then
                GUICtrlSetData($PauseResume, "&Weiter")
                While GUICtrlRead($PauseResume) <> "&Pause"
                    _MySleep(50)
                WEnd
            Else
                GUICtrlSetData($PauseResume, "&Pause")
            EndIf
    EndSwitch
EndFunc   ;==>_EventHandler

Func _MySleep($iMSec)
    Local $iStart = TimerInit()
    Do
        _EventHandler()
    Until TimerDiff($iStart) >= $iMSec
EndFunc   ;==>_MySleep

it's possible that script is working and the buttons also working when specific Msg is fired. But this technique will not proberly work with MsgBoxes or RunWait, ShellExecuteWait and other functions you couldn't split in small pieces. So i full ack with M23, post your script. 

Link to comment
Share on other sites

I write a litte example to describe for my loop problem here. Generally, we can click button Close and exit at any time. But when a 2nd loop is running, we click button Close and must wait the loop finished to exit.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("guioneventmode",1)

GUICreate("Loop Problem", 200, 200, 192, 124)
GUICtrlCreateButton("Close", 72, 152, 75, 25)
GUICtrlSetOnEvent(-1,"close")
GUICtrlCreateButton("Run 2nd Loop", 72, 56, 75, 65)
GUICtrlSetOnEvent(-1,"runloop")
$idLabel = GUICtrlCreateLabel("0", 72, 24, 74, 17)
GUISetState(@SW_SHOW)

while 1
    Sleep(10)
WEnd

Func runloop()
    For $i=1 to 100000
        GUICtrlSetData($idLabel,$i)
        $i+=1
        Sleep(10)
    Next
EndFunc

Func close()
    Exit
EndFunc

 

Link to comment
Share on other sites

  • Moderators

hiepkhachsg,

I thought that might be the problem. The Interrupting a running function tutorial in the Wiki explains why this happens and how you can get around it.

Come back if you still have problems after having read the tutorial.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

A method not explained in wiki, building a pseudoloop with Adlib:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("guioneventmode",1)

GUICreate("Loop Problem", 200, 200, 192, 124)
GUICtrlCreateButton("Close", 72, 152, 75, 25)
GUICtrlSetOnEvent(-1,"close")
GUICtrlCreateButton("Run 2nd Loop", 72, 56, 75, 65)
GUICtrlSetOnEvent(-1,"startloop")
$idLabel = GUICtrlCreateLabel("0", 72, 24, 74, 17)
GUISetState(@SW_SHOW)

while 1
    Sleep(10)
WEnd

Func startloop()
    AdlibRegister('runloop',10)
EndFunc

Func runloop()
    Local Static $i=1
    If $i<100001 Then
        GUICtrlSetData($idLabel,$i)
        $i+=1
    else
        $i=1
        AdlibUnRegister('runloop')
    EndIf
EndFunc

Func close()
    AdlibUnRegister('runloop')
    Exit
EndFunc

 

Edited by AutoBert
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...