Jump to content
sudeepjd

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

Share this post


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

Share this post


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

Share this post


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.

Share this post


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) ?

Share this post


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

 

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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.

  • Similar Content

    • 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!
    • By sudeepjd
      I am building an application which needs a child panel in the GUI Control that needs to be scrolled as it contains controls that extend beyond the panel height. But I also need to have the users to be able to Tab through those controls. I don't seem to be able to to both working together. 
      The Tabstops can be allowed on children by using $WS_EX_CONTROLPARENT and the Scrollbar creation using the GUIScrollBar.au3.
      If I set the $WS_EX_CONTROLPARENT it drags the entire child and does not allow the scroll, but if I remove it, the Scroll works but the tabstops don't. Please see the below sample application that can help reproduce this problem.
       
      #include <GUIConstants.au3> #include <GUIScrollbars.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Global $width=500, $height=500, $titel="Tabtest in Childwindow" ; create the parentwindow $mainwindow = GUICreate($titel, $width, $height, -1, -1) GUISetBkColor(0x00ffff) ; show the parentwindow GUISetState(@SW_SHOW,$mainwindow) ; check on screen sleep(1000) ; create the childwindow with the scrollbars active ->TABSTOPS Dont work $childwindo1 = GUICreate("child", 220,$height, 10 ,0,$WS_CHILD, -1 ,$mainwindow) _GUIScrollBars_Init($childwindo1, 100, 100) GUISetBkColor(0xff0000, $childwindo1) GUISetState(@SW_SHOW, $childwindo1) $input_1 = GUICtrlCreateInput("Scroll Works",10,10) $input_2 = GUICtrlCreateInput("Tab Does Not",10,40) ; create the childwindow with the scrollbars active ->TABSTOPS Work, Scroll does not because window moves $childwindo2 = GUICreate("child", 220, $height, 240 ,0,$WS_CHILD, $WS_EX_CONTROLPARENT ,$mainwindow) _GUIScrollBars_Init($childwindo2, 100, 100) GUISetBkColor(0xff0000, $childwindo2) GUISetState(@SW_SHOW, $childwindo2) $input_3 = GUICtrlCreateInput("Tab Works",10,10) $input_4 = GUICtrlCreateInput("Scroll Does Not",10,40) GUIRegisterMsg($WM_VSCROLL, "WM_VSCROLL") ; register close GUISetOnEvent($GUI_EVENT_CLOSE, "close_it",$mainwindow) ;loop While 1 Sleep(100) ; Idle around WEnd Func close_it() ; exit application GUIDelete($mainwindow) exit EndFunc $childwindo = GUICreate("child",$width,$height,0,0,$WS_CHILD,-1,$mainwindow) GUISetBkColor(0xff0000) GUISetState(@SW_SHOW,$childwindo) Func WM_VSCROLL($hWnd, $iMsg, $wParam, $lParam) #forceref $iMsg, $wParam, $lParam Local $iScrollCode = BitAND($wParam, 0x0000FFFF) Local $iIndex = -1, $iCharY, $iPosY Local $iMin, $iMax, $iPage, $iPos, $iTrackPos For $x = 0 To UBound($__g_aSB_WindowInfo) - 1 If $__g_aSB_WindowInfo[$x][0] = $hWnd Then $iIndex = $x $iCharY = $__g_aSB_WindowInfo[$iIndex][3] ExitLoop EndIf Next If $iIndex = -1 Then Return 0 ; Get all the vertial scroll bar information Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT) $iMin = DllStructGetData($tSCROLLINFO, "nMin") $iMax = DllStructGetData($tSCROLLINFO, "nMax") $iPage = DllStructGetData($tSCROLLINFO, "nPage") ; Save the position for comparison later on $iPosY = DllStructGetData($tSCROLLINFO, "nPos") $iPos = $iPosY $iTrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos") Switch $iScrollCode Case $SB_TOP ; user clicked the HOME keyboard key DllStructSetData($tSCROLLINFO, "nPos", $iMin) Case $SB_BOTTOM ; user clicked the END keyboard key DllStructSetData($tSCROLLINFO, "nPos", $iMax) Case $SB_LINEUP ; user clicked the top arrow DllStructSetData($tSCROLLINFO, "nPos", $iPos - 1) Case $SB_LINEDOWN ; user clicked the bottom arrow DllStructSetData($tSCROLLINFO, "nPos", $iPos + 1) Case $SB_PAGEUP ; user clicked the scroll bar shaft above the scroll box DllStructSetData($tSCROLLINFO, "nPos", $iPos - $iPage) Case $SB_PAGEDOWN ; user clicked the scroll bar shaft below the scroll box DllStructSetData($tSCROLLINFO, "nPos", $iPos + $iPage) Case $SB_THUMBTRACK ; user dragged the scroll box DllStructSetData($tSCROLLINFO, "nPos", $iTrackPos) EndSwitch ; // Set the position and then retrieve it. Due to adjustments ; // by Windows it may not be the same as the value set. DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS) _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO) _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO) ;// If the position has changed, scroll the window and update it $iPos = DllStructGetData($tSCROLLINFO, "nPos") If ($iPos <> $iPosY) Then _GUIScrollBars_ScrollWindow($hWnd, 0, $iCharY * ($iPosY - $iPos)) $iPosY = $iPos EndIf Return $GUI_RUNDEFMSG EndFunc ;==>WM_VSCROLL  
      Any help to get both working together is appreciated.
      Thanks,
      Sudeep.
    • By Atrax27
      I have a webpage that I would like to Focus the Input on a particular field, which is not automatically set as the initial input.
       
      Website:
      https://fiscaloffice.summitoh.net/PropertyTaxValues/PayTaxCC.htm
       
      I want to just simply use the Send(“154xxx”) into the “Parcel” field, then Send(“{ENTER}”) without cycling through tab presses each time before getting to the correct inputbox, where number of tab press numbers might change depending on a few things (using either IE, chrome, etc). On most websites I try this with, the “Login ID” field is the first one that the cursor jumps to so all I do is just start a Send command, but here I can’t do that since it’s not the initial cursor location.
       
      I’ve read a few things on the forums about “_WinAPI_SetFocus” but that appears to work on AutoIT generated form fields only (see below code), I don’t know how to translate it for website use, thanks for any help!
       
      #include <GUIConstantsEx.au3> #Include <WinAPI.au3>   $hGUI = GUICreate("Test", 500, 500) $hInput = GUICtrlCreateInput("", 10, 10, 400, 20) GUICtrlCreateButton("Test", 10, 100, 80, 30) GUICtrlSetState(-1, $GUI_FOCUS)   GUISetState() Sleep(5000) _WinAPI_SetFocus(ControlGetHandle("Test", "", $hInput)) While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch WEnd  
         
    • By paradox109
      Hello, 
      I have A simple question about http request. What would be the fastest way to send mupltiple http request at the same time with autoit? The only way i figured  out was to to start multiple processes. This way works fine but its not really a good way. What user would like to see 15 processes running in the background at the same time. I know multithread is also not available in autoit.
×
×
  • Create New...