Jump to content

Hold Main thread loop while Child Window running


sudeepjd
 Share

Recommended Posts

I am looking for a way to pull up a Child GUI Window that users can enter information into and return that information to the main for loop which is running off an array. However, I have been unable to do so because the For loop continues even though the child window is open. If I put in another while loop inside the child window function, I am not able to poll the windows for the events looking for the close button getting clicked.

I have put together a simple test application that shows this. Any help with holding the main loop while the child window is open and returning when the Close button is clicked is appreciated.

In the below example, the child window contains a single text box, however, on my main application the Child GUI is much more complex with multiple pieces of information being returned.

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

AutoItSetOption("GUIOnEventMode", 1)

Global $hGUI, $hChild
Global $childClose = False
Global $childValue

OpenMainGUI()

While Sleep(100)
    ConsoleWrite("Main Loop")
WEnd

Func OpenMainGUI()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 100)

    $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Events
    GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitMain", $hGUI)
    GUICtrlSetOnEvent($btnMain, "createChildren")

EndFunc   ;==>Example

Func createChildren()
    For $i = 1 to 5
        createChild($i)
        ConsoleWrite($i & " - " &  $childValue & @CRLF)
    Next
EndFunc

Func createChild($valInp)
    $childClose = False
    $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI)

    $txtOperation  = GUICtrlCreateInput($valInp, 10, 10, 100, 20)
    $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30)

    GUICtrlSetOnEvent($btnCloseChild, "_ExitChild")

    ; Display Child
    GUISetState(@SW_SHOW)

    ;Wait here till Close button is clicked
    While $childClose = False
        ConsoleWrite("Stuck in this loop..." & @LF)
        Sleep(100)
        $aMsg = GUIGetMsg(1)
        If $aMsg[0] >0 Then _ArrayDisplay($aMsg)
    Wend
EndFunc

Func _ExitMain()
    Exit
EndFunc

Func _ExitChild()
    ConsoleWrite("Exit Child Called")
    $childValue = GUICtrlRead($txtOperation)
    $childClose = True
    GUIDelete($hChild)
EndFunc

Thanks in advance for any help offered.

My other alternative is to create a separate EXE for the child window and use ShellExecuteWait to wait for the child window to close before the loop continues, but I am hoping to avoid doing that.

Edited by sudeepjd
Link to comment
Share on other sites

There is multiple problems with your script.

1-  You cannot have 2 embedded loops within an overall GUI

2- GUIGetMsg does not work on AutoItSetOption("GUIOnEventMode", 1)

3- You are launching 5 child GUIs, but you only keep one reference (to the last)

You should revise your design, I do not see the need to start so many child.  And using shell to run your child windows and exchanging info from child to main, well, good luck to you.

My suggestion make it work with a single child first.  If you absolutely need multiple child, you will need to use an array to hold all child controls.

Please see tutorial : https://www.autoitscript.com/wiki/Managing_Multiple_GUIs

Edited by Nine
Link to comment
Share on other sites

@Nine Thanks for looking at it. I do not want 5 child GUI's.. 

What I need is to have 1 Child GUI that will wait for it to get closed, before the control is passed back to the main window.

My explanation, now that I read it again, is not very clear.... sorry.

What I need is a method to start a Child GUI in a loop, The Child GUI will get some information passed to it from the main script (I am pulling that information from IE which the user needs, the user then enters the requisite information into the child window and clicks Close and then the control passes back to the main window which then proceeds to get the information for the next part.

What I am looking for is a method to have the main loop wait while the user is entering the data on the child window, but I am not able to figure out how to get there.

Once Again, thanks for looking at this for me. I have updated the above code to have a single Child Window instance instead of Creating and Deleting the ChildGUI. But the wait is still a problem.

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

AutoItSetOption("GUIOnEventMode", 1)

Global $hGUI, $hChild
Global $childClose = False
Global $childValue

OpenMainGUI()
CreateChildGUI()

While Sleep(100)
    ConsoleWrite("Main Loop")
WEnd

Func OpenMainGUI()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 100)

    $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Events
    GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitMain", $hGUI)
    GUICtrlSetOnEvent($btnMain, "createChildren")

EndFunc   ;==>Example

Func CreateChildGUI()
    $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI)

    Global $txtOperation  = GUICtrlCreateInput("", 10, 10, 100, 20)
    Global $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30)

    GUICtrlSetOnEvent($btnCloseChild, "_ExitChild")
EndFunc

Func createChildren()
    For $i = 1 to 5
        GUICtrlSetData($txtOperation, $i)
        GUISetState(@SW_SHOW, $hChild)
        ;THIS IS WHERE THE CONTROL SHOULD WAIT BEFORE CHILD IS CLOSED or $childClose = True
        ConsoleWrite($i & " - " &  $childValue & @CRLF)
    Next
EndFunc

Func _ExitMain()
    Exit
EndFunc

Func _ExitChild()
    ConsoleWrite("Exit Child Called")
    $childValue = GUICtrlRead($txtOperation)
    $childClose = True
    GUIDelete($hChild)
EndFunc

 

Edited by sudeepjd
Link to comment
Share on other sites

I think I figured it out... Your point of you cannot have 2 embedded loops put me in the right direction.. The code below works the way I want it to..

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

AutoItSetOption("GUIOnEventMode", 1)

Global $hGUI, $hChild
Global $childClose = False
Global $rIndex
Global $routeData[][3] = [[1, 0, False], [2, 0, False], [3, 0, False]]

OpenMainGUI()

While Sleep(100)
    ;ConsoleWrite("Main Loop")
WEnd

Func OpenMainGUI()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 100)

    $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Events
    GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitMain", $hGUI)
    GUICtrlSetOnEvent($btnMain, "createChildren")

EndFunc   ;==>Example

Func CreateChildGUI($iVal)
    $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI)

    Global $txtOperation  = GUICtrlCreateInput($iVal, 10, 10, 100, 20)
    Global $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30)

    GUICtrlSetOnEvent($btnCloseChild, "_ExitChild")

    GUISetState(@SW_SHOW, $hChild)
EndFunc

Func createChildren()
    ConsoleWrite("createChildren" & @CRLF)
    $showChild = False
    For $i = 0 to UBound($routeData)-1
        ConsoleWrite($routeData[$i][2] & @CRLF)
        If $routeData[$i][2] = False Then
            $rIndex = $i
            CreateChildGUI($i)
            $showChild = True
            ExitLoop
        EndIf
    Next

    If $showChild = False Then _ArrayDisplay($routeData)
EndFunc

Func _ExitMain()
    Exit
EndFunc

Func _ExitChild()
    ConsoleWrite("Exit Child Called" & @CRLF)
    $routeData[$rIndex][1] = GUICtrlRead($txtOperation)
    $routeData[$rIndex][2] = True
    GUIDelete($hChild)
    Call("createChildren")
EndFunc

However, if there is a better way to do it, instead of handling the flow transfer this way, would be helpful.

Link to comment
Share on other sites

Not exactly sure what are your intentions here.  ATM, the behavior of your script is that when you click open from the main GUI, it will show 3 times the same child GUI.  After the 3 GUIs are shown and closed, there is nothing more you can do except quitting the script.  Is that what you expect (want) ?

Link to comment
Share on other sites

Or you can do it this way

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $hGUI, $hChild = -999
Global $txtOperation, $btnCloseChild

OpenMainGUI()

Func OpenMainGUI()
    $hGUI = GUICreate("Example", 400, 100)
    Local $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30)
    Local $aMax = 4
    Local $routeData[$aMax - 1][2]
    Local $counter = 1

    GUISetState(@SW_SHOW, $hGUI)

    Local $nMsg 
    While 1
        $nMsg = GUIGetMsg(1)
        Switch $nMsg[1]
        
            Case $hGUI
                Switch $nMsg[0]
                    Case $GUI_EVENT_CLOSE 
                        ExitLoop
                    
                    Case $btnMain
                        ($counter < $aMax ? CreateChildGUI($counter) : _ArrayDisplay($routeData) )

                EndSwitch
            
            Case $hChild
                Switch $nMsg[0]
                    Case $btnCloseChild
                        $routeData[$counter - 1][0] = $counter - 1
                        $routeData[$counter - 1][1] = GUICtrlRead($txtOperation)
                        GUIDelete($hChild)
                        $counter += 1
                        If $counter < $aMax Then CreateChildGUI($counter)
                EndSwitch
            
        EndSwitch
    WEnd

    GUIDelete($hGUI)
EndFunc 


Func CreateChildGUI($iVal)
    $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI)

    $txtOperation  = GUICtrlCreateInput($iVal, 10, 10, 100, 20)
    $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30)
    GUISetState(@SW_SHOW, $hChild)
EndFunc

 

Link to comment
Share on other sites

11 hours ago, Nine said:

Not exactly sure what are your intentions here.  ATM, the behavior of your script is that when you click open from the main GUI, it will show 3 times the same child GUI.  After the 3 GUIs are shown and closed, there is nothing more you can do except quitting the script.  Is that what you expect (want) ?

Yes.. that is what I am looking for.. The data being requested and entered will be different each time, from the same GUI window.

Kind of like a custom Inputbox feature. In an Inputbox the script waits for the user to enter the data and then passes the input back into the script. Since I need to have the user enter a lot more information, the Child GUI needs to be a custom one.. and the main script needs to wait until the user has finished entering the data into the child GUI and hit Close.

I found what I was looking for on another post.. And your note on GUIGetMsg does not work on OnEventMode and that the _ArrayDisplay via the __ArrayDisplay_Share also works the same way.

I need to disable and re-enable GUIOnEventMode to get what I am looking for to work.

Here is my working code sample, for anyone else who comes along this type of a problem.

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

AutoItSetOption("GUIOnEventMode", 1)

Global $hGUI

OpenMainGUI()

While Sleep(100)
    ;Main Loop
Wend

Func OpenMainGUI()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 100)

    $btnMain = GUICtrlCreateButton("Open", 10, 10, 100, 30)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Events
    GUISetOnEvent($GUI_EVENT_CLOSE, "_ExitMain", $hGUI)
    GUICtrlSetOnEvent($btnMain, "createChildren")

EndFunc   ;==>OpenMainGUI

Func createChildren()
    For $i = 1 to 5
        $out = createChild($i)
        ConsoleWrite($i & " - " &  $out & @CRLF)
    Next
EndFunc ;==>createChildren()

Func createChild($valInp)
    ;Move On Event Mode to MessageLoop Mode
    AutoItSetOption('GUIOnEventMode', 0)
    $hChild = GUICreate("Child GUI", 210, 72, -1, -1, -1, -1, $hGUI)

    $txtOperation  = GUICtrlCreateInput($valInp, 10, 10, 100, 20)
    $btnCloseChild = GUICtrlCreateButton("Close", 10, 40, 100, 30)

    ; Display Child
    GUISetState(@SW_SHOW, $hChild)

    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
        Case $GUI_EVENT_CLOSE
            $retValue = "Skip"
            ExitLoop
        Case $btnCloseChild
            $retValue = GUICtrlRead($txtOperation)
            ExitLoop
        EndSwitch
    WEnd
    GUIDelete($hChild)

    ;Restore On Event Mode
    AutoItSetOption('GUIOnEventMode', 1)
    Return $retValue
EndFunc ;==>CreateChild

Func _ExitMain()
    Exit
EndFunc

 

@Nine Thank you for your time. You and the other AutoIt experts here on the forum are a blessing. I have been working with AutoIT for over 10 years now and I get stuck with it sometimes and the community here helps a lot.

Edited by sudeepjd
Link to comment
Share on other sites

@jugador Thanks.. However, my entire program is in EventMode over the MessageLoop mode.. and it is quite large. And I need to hand back the input from the child GUI so this may not work directly for me... But the combination does.

Thanks again though.

Edited by sudeepjd
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

×
×
  • Create New...