Jump to content

Hold Main thread loop while Child Window running


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

  • Similar Content

    • By WhaleJesus
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> ; Create Data Folder if it doesn't exist yet If FileExists(@ScriptDir & "\Data") Then Else ShellExecute(@ScriptDir) DirCreate(@ScriptDir & "\Data") EndIf ; Playlist Name & location input Global $playlistnameinput = InputBox("Playlist", "Enter The playlist name", _ "Name") Global $playlistlocationinput = InputBox("Location", "Specify where you would like the playlist folder to be stored", @ScriptDir & "\Playlists\" & $playlistnameinput) ; Create file in Data folder and other vars Global $sDataFile = @ScriptDir & "\Data\Data.txt" Global $DataHandle = FileOpen($sDataFile, 1) Global $DataFileLine = FileReadLine($sDataFile, 1) FileClose($DataFileLine) MsgBox(0, "", $DataFileLine, 10) ; Prove it exists If FileExists($sDataFile) Then _FileWriteToLine($DataHandle, $DataFileLine, $playlistnameinput, True, True) $DataFileLine += 1 _FileWriteToLine($DataHandle, 1, $DataFileLine, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sDataFile & "Does not exist") EndIf Global $sPDataFile = @ScriptDir & "\Data\" & $playlistnameinput & "_Data.txt" Global $PDataHandle = FileOpen($sPDataFile, 1) If FileExists($sPDataFile) Then _FileWriteToLine($PDataHandle, 1, $playlistnameinput, True, True) _FileWriteToLine($PDataHandle, 2, $playlistlocationinput, True, True) Else MsgBox($MB_SYSTEMMODAL, "Error", "File " & $sPDataFile & "Does not exist") EndIf _FileWriteToLine stopped working and i don't know what it is in my code that's causing this, please help
    • By DannyJ
      $sCommands1 = 'powershell.exe Get-ChildItem' $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2) $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) ConsoleWrite("$sOutput") ConsoleWrite($sOutput) ConsoleWrite(@CRLF) $aOutput = stringsplit($sOutput ,@LF , 2) For $i=0 To  UBound($aOutput) - 1 Step 1     ConsoleWrite($aOutput[$i]) Next The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.
      I have already read some forum answers here, and I have already tried these commands:
       
      Are there any way to use the $aOutput variable like in PowerShell:
      PowerShell:
      $a = Get-ChildItem $a.Mode I imagine this in AutoIt  $aOutput
      ConsoleWrite($aOutput[i].Mode) Or if I split this command into 2 dimension like:
      For $i To UBound($aOutput)-1 Step 1 ConsoleWrite($aOutput[$i][1]) ConsoleWrite($aOutput[$i][2]) Next  
    • By DannyJ
      If I run this code, it works perfectly
      $CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-ChildItem',@DesktopDir, @SW_SHOW) But this code
      $CmdPid = Run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit " & 'Get-RDUserSession',@DesktopDir, @SW_SHOW) I get this error:
      Get-RDUserSession : The term 'Get-RDUserSession' is not recognized as the name of a cmdlet, function, script file, or o perable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try aga in. If I try run the command Get-RDUserSession  in normal PowerShell (started from windows start menu) the command works perfectly.
      But If I run with AutoIt I get the above mentioned error .
      Any ideas?
    • By Automania
      Hi all,
      I haven't used AutoIt in more than 10 years and I am sure a lot has improved since that long time. I hope you can give me some suggestions on my approach.
      Task: I need to extract user data (for around 1700 users) from a website tool. That tool shows an output in a table on the website. However, no export feature is available and I need the data in an Excel file, such as:
      username, serial number (of a laptop), ID number (of laptop) and some more
       
      With my knowledge from 2009 I would do this:
      1) use _IEextract with each username in the url to get the whole source code of the website with the user's data summary
      2) Work with lots of regexpressions to extract each data piece, save them into variables/array
      3) Write variable values into an Excel file
      4) rinse repeat 1700 times
       
      The relevant line for step 3 looks like this:
      <td class="resultcell"><span class="new">2021-03-23 11:05:00</span></td><td class="resultcell">Hostname-1234</td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=serial%20numbers&criteria=123456">123456</a></td><td class="resultcell">0987654/td><td class="resultcell"><a href="?&Search=Search&result=summarized%20history&field=usernames&criteria=myusername">myusername</a> and so on.. so here it would be Hostname-1234, 0987654 and myusername that I would need to extract.

      Although this may work it does not appear very efficient and would take a while. So I am happy for an alternate approach. Preferably, without using additional exe binary files due to company policies besides AutoIt itself.
    • By SEuBo
      Hi!
      I am just getting started with C and C++. I have created a pretty simple C code which is calling a dll function.
      When I compile and run, I get the appropriate Output. So it works fine.

       
      Now I would want to transform that to AutoIt. -> I would like to call the "RfcOpenConnection" function from AutoIt - but whatever I try with DLLCall, I can not get it to work. 
      Can someone point me in the right direction? DLL, C Sourcecode and compiled exe are attached too large to be attached, so they're uploaded here: 
      https://drive.google.com/file/d/12CUSsISl0mojiMCNxKjps1Sdoox3JlCX/view?usp=sharing
       
      Thanks a bunch!
×
×
  • Create New...