Jump to content
stick3r

While loop calling GUI() several times

Recommended Posts

Hi,

I have a code, where I start my script with func GUI(), hide GUi with GUISetState(@SW_Hide) and then call Main(), doing stuff in Main() and then in some cases I call GUI() again.

Func GUI()
    While 1
        GUISetState(@SW_SHOW)
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                GUISetState(@SW_Hide)
                Main()
        EndSwitch
    WEnd
EndFunc

Func Main()
    .
    .
    .
    GUI()
EndFunc

My question is:

How many While loops are looping after I call GUI() second time from Main()? Am I creating second While 1..... loop, or does it open the same loop?

 

And what if I Return something from func GUI(). Then I believe I exit func completely - therefore While loop ends for sure and next time I call Main, it starts func GUI() from scratch?

Func Main()
   $arrGuiReturnData = GUI()
   .
   .
    Main()
    .
    .
EndFunc

Func GUI()
    While 1
        GUISetState(@SW_SHOW)
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                GUISetState(@SW_HIDE)
                Return $arrGuiReturnData
        EndSwitch
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites

Only one loop is performing at a given time.  You are using recursion which you shouldn't use if you do not understand it completely. There a risk of stack overflow...

In your first snippet, what happen is the first loop is stopped at the call to Main ().  A second loop is started when you call GUI (). 

BTW, GUISetState(@SW_SHOW) should be outside the loop, there is no need to constantly call this statement.

Share this post


Link to post
Share on other sites
Main()

Func Main()
    While True ; main nonGUI loop
        .
        .
        If ... Then $arrGuiReturnData = GUI()
        .
        .
        Sleep(10)
    WEnd
EndFunc

Func GUI()
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                GUISetState(@SW_HIDE)
                Return $arrGuiReturnData
        EndSwitch
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites

Basically, what I need to do is, in case of wrong input from user, I want to come back to GUI() and give a chance for user to correct input and start again.

In theory user might input some info many times in the row to GUI(), which can only be verified in the system. i.e. I check if input is 10 digits, but if it is legit number, only a system can tell.

Something like below:

Func Main()
   $arrGuiReturnData = GUI()
   $returnData = DataFromSystem($arrGuiReturnData)
   If @error = 1 Then Main()
EndFunc

Func GUI()
    While 1
        GUISetState(@SW_SHOW)
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                GUISetState(@SW_HIDE)
                Return $arrGuiReturnData
        EndSwitch
    WEnd
EndFunc

What is the best way to come back to GUI() and give a chance to user to correct input?

Share this post


Link to post
Share on other sites
Main()

Func Main()
    While True ; main nonGUI loop
        .
        .
        If ... Then 
            While 1 ; loop until data are correct
                $arrGuiReturnData = GUI()
                $returnData = DataFromSystem($arrGuiReturnData)
                If @error = 0 Then ExitLoop
            WEnd
        EndIf
        .
        Sleep(10)
    WEnd
EndFunc

Func GUI()
    GUISetState(@SW_SHOW)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $btnGo
                GUISetState(@SW_HIDE)
                Return $arrGuiReturnData
        EndSwitch
    WEnd
EndFunc

 

Edited by Zedna

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...