Jump to content

Running lengthy task in GUI?


Go to solution Solved by Andreik,

Recommended Posts

Hello,

I just need to display a listbox, run a for loop that could take minutes and just adds a few items to the list, and then let the user close the application.

What would be the right way to do this? It looks like the OnEventMode() would be a better way to keep the GUI responsive.

Thank you.

;Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 449, 279, 114)
$List1 = GUICtrlCreateList("", 8, 8, 601, 422)
;GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEButton")
;GUICtrlSetOnEvent($iOKButton, "OKButton")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;~ While 1
;~         Sleep(100) ; Sleep to reduce CPU usage
;~ WEnd

;~ Func CLOSEButton()
;~         ; Note: At this point @GUI_CtrlId would equal $GUI_EVENT_CLOSE,
;~         ; and @GUI_WinHandle would equal $hMainGUI
;~         MsgBox($MB_OK, "GUI Event", "You selected CLOSE! Exiting...")
;~         Exit
;~ EndFunc   ;==>CLOSEButton 
;~ Exit

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    For $picindx = 1 to UBound($aArray) -1
        ;lengthy task
    Next
    ;GUICtrlSetData($Form1, "Done.")
    WinSetTitle($Form1, "Done.", "")
    ConsoleWrite("Done." & @CRLF)
WEnd

 

Link to comment
Share on other sites

Thanks. What files do I need, and would you have a "Hello, World!" I could try fast and see if it solves the problem?

Example_Fork_Broadcast.au3
Example_Fork_BroadcastWithFMIPC.au3
Example_Fork_Events.au3
Example_Fork_FileMonitor.au3
Example_Fork_FileMonitor_NoGui.au3
Example_Fork_Reciever.au3
Example_Fork_TCP.au3
FMIPC.au3
Fork.au3
ForkDbgAid.au3
ForkDbgWr.au3
MailSlot.au3

Link to comment
Share on other sites

hmm, maybe this is less advanced and gives you a clue :

Example()
Func Example()
    Local $sHello = "Hello cru" & "el world" ; ..this way is safer ; discovered after debugging
    If StringInStr($CmdLineRaw, "/bangbang") Then
        If WinWait("", $sHello, 5) Then
            MsgBox(262144, @ScriptName, "Bye cruel world", 1)
            WinClose("", $sHello)
        Else
            MsgBox(262144, @ScriptName, "oops ?", 2)
        EndIf
    Else
        ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '" /bangbang')
        MsgBox(262144, @ScriptName, $sHello)
    EndIf
EndFunc

..basically you can have a script handle other scripts in whatever creative ways you can apply within your "arsenal of code knowing" / experience :) 

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

5 hours ago, littlebigman said:

The GUI still closes:

For blah
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ;Exit
            ExitLoop
    EndSwitch
    ;do stuf
Next
;gone!

Alternatively, is there some Refresh() command that I could call within the GUI loop to keep the application responsive?

You're joking. The GUI close if you close it or if the script ends. You must be more specific about what do stuff and lengthy task means.

When the words fail... music speaks.

Link to comment
Share on other sites

  • Solution

I still don't get what do you mean to keep the GUI responsive. Something like that?

$hMain = GUICreate('Whatever', 400, 400)
$cListBox = GUICtrlCreateList('', 10, 10, 390, 350)
$cButton = GUICtrlCreateButton('Click me', 100, 365, 200, 30)
GUISetState(@SW_SHOW, $hMain)

While True
    ProcessMessages()
    AdlibRegister('ProcessMessages', 30)
    For $Index = 1 To 1000
        ; If a loop doesn't take seconds
        ; you can remove adlib and process
        ; messages here
        ; ProcessMessages()
        GUICtrlSetData($cListBox, $Index)
        Sleep(500)
    Next
    AdlibUnRegister('ProcessMessages')
    Sleep(10)
WEnd

Func ProcessMessages()
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $cButton
            ConsoleWrite('Hi there!!' & @CRLF)
    EndSwitch
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Thank you!

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 449, 279, 114)
$List1 = GUICtrlCreateList("", 0, 52, 622, 396)
$Label1 = GUICtrlCreateLabel("Label1", 8, 8, 428, 31)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Func ProcessGUI()
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
EndFunc

For blah
    ProcessGUI() ;to keep the GUI responsive
    ;do stuff
Next

WinSetTitle($Form1, "", "Done!")

;So the app doesn't close after the for loop
While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Edited by littlebigman
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...