Jump to content
Sign in to follow this  
Tersion

Handle GUI events when Do...Until is working.

Recommended Posts

Here test example:

Spoiler
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$hMainForm = GUICreate("Form1", 167, 59, 192, 124)
$idLabel = GUICtrlCreateLabel("Label1", 5, 5, 36, 17)
$idStartButton = GUICtrlCreateButton("Start", 5, 25, 75, 25)
$idStopButton= GUICtrlCreateButton("Stop", 85, 25, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $iStep = 0

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

Func Update()
    Do
        Sleep(1000)
        $iStep += 1
        GUICtrlSetData($idLabel, $iStep)
    Until $iStep = 10
EndFunc

 

I want to be able to update Label control and can handle Stop button event when Do...Until is working. How can I achieve that?

Share this post


Link to post
Share on other sites

You could do something like this.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$hMainForm = GUICreate("Form1", 167, 59, 192, 124)
$idLabel = GUICtrlCreateLabel("Label1", 5, 5, 36, 17)
$idStartButton = GUICtrlCreateButton("Start", 5, 25, 75, 25)
$idStopButton = GUICtrlCreateButton("Stop", 85, 25, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


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

Func Update()
    Local $iStep = 0, $Timer = TimerInit()
    Do
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $idStopButton
                Return
        EndSwitch
        If TimerDiff($Timer) >= 1000 Then
            $iStep += 1
            GUICtrlSetData($idLabel, $iStep)
            $Timer = TimerInit()
        EndIf
    Until $iStep >= 1000
EndFunc   ;==>Update

This takes out the Sleep function, which would affect the buttons in the Do loop, and replaces it with a timer.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Danp2,

Thanks one more time!

For me, your advice with AdlibRegister appeared to be simpler to incorporated to my project (mainly it's download manager for additional Left 4 Dead 2 campaigns), and now I can show simultaneously multiple progress status of downloads in ListView control column. Here the .gif:

P.S. Some translation:

Статус - Status

Загрузить - Download

Загрузка: 5% - Downloading: 5%

Ожидание загрузки - Waiting for download

Загружено - Downloaded

download_gui_test.gif.a2f94b278eb115eb6fcf337463161f59.gif

Share this post


Link to post
Share on other sites

Tersion,

Quote

it's download manager for additional Left 4 Dead 2 campaigns

Is this purely a file manager or does it interact with the game itself? Please read the Forum rules to see why I ask.

 

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23,

Oh, I see:

1. Do not ask for help with AutoIt scripts, post links to, or start discussion topics on the following subjects:

  • Launching, automation or script interaction with games or game servers, regardless of the game.

I understand that point and my previous post with a mentions of the game related things was last. But can I explain?

My program just download publicly available .zip archives (which contains .vpk files inside with additional campaigns for the game) and unpack them to game folder. That's all. As I know, in licensed (Steam version) of Left 4 Dead 2 you have two legit options to install some kind of addons to the game. First - use Workshop, or second - manual copy .vpk file to addons folder. After that you manually launch the game and it's processed newly copied .vpk files. So the whole process of installing additional campaigns I cant to name: "regardless of the game".

And I use second option almost from 2013. Now I want to add some automation to this process.

judge of course to you (or any other moderator) if I violate forum rules, but as already I say, I understand that kind of Forum Rules and in the future I will avoid any mention of this kind and any other prohibited topics.

Edited by Tersion

Share this post


Link to post
Share on other sites

Tersion,

Quote

My program just download publicly available .zip archives [...] and unpack them to game folder

Given that I will leave the thread open - but please do make sure that you respect the rules in any future threads.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi everybody,
This thread is really interesting and I would like to share some thoughts with you.
I thought that the "On Event" mode would have solved Tersion's problem in a snap. After all, what seems more natural than clicking on a Stop button, triggering immediately the Stop Event associated function and taking control of the situation ?

But unfortunately it's not that simple : when On Event is activated, the Stop button won't do anything until the loop ends in the Start button code,

Then I read some great web pages concerning this question, thanks to AdmiralAlkex and Melba23 for their so interesting comments here, at the very bottom of this web page :
https://www.autoitscript.com/forum/topic/124262-help-with-guioneventmode/

Not forgetting this very useful wiki page :
https://www.autoitscript.com/wiki/Interrupting_a_running_function

Then only I could rework Tersion's script, in On Event mode... and it works fine :)
Thanks for reading and hats off to the contributors of this Forum for their help during so many years.

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled

$hMainForm = GUICreate("Form1", 167, 59, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "ClosePressed")

$idLabel = GUICtrlCreateLabel("Label1", 5, 5, 36, 17)

$idStartButton = GUICtrlCreateButton("Start", 5, 25, 75, 25)
GUICtrlSetOnEvent(-1, "StartFlag")

$idStopButton = GUICtrlCreateButton("Stop", 85, 25, 75, 25)
GUICtrlSetOnEvent(-1, "StopPressed")

GUISetState(@SW_SHOW)

; Declare a flag to be used in OnEvent functions
Global $fStartFlag = False

While 1
    Sleep(10)
   ; Check if the flag has been set by the StartFlag OnEvent function
   If $fStartFlag Then
      ; Now start the "real" function from within the main code
      StartPressed()
   EndIf
WEnd

Func StartFlag()
   ; Set the flag within the OnEvent function
     $fStartFlag = True
EndFunc   ;==>StartFlag

Func StartPressed()
   Local $iStep = 0
   Do
      Sleep(1000)
      ; Check the flag status, in case Stop was pressed
      If $fStartFlag = False Then
         Return
      EndIf
      $iStep += 1
      GUICtrlSetData($idLabel, $iStep)
   Until $iStep = 5
   $fStartFlag = False
EndFunc   ;==>StartPressed

Func StopPressed()
   $fStartFlag = False
EndFunc   ;==>StopPressed

Func ClosePressed()
   Exit
EndFunc   ;==>ClosePressed

 

Share this post


Link to post
Share on other sites

pixelsearch,

Thanks for your thoughts about the problem and for the links you provided. I tested your rework of my example and it's really works nicely and looks not that bad as for understanding. I never used "On Event" mode before, but now I understand that for some tasks it's could be a solution, so I will begin to study more about how to work in this mode.

Share this post


Link to post
Share on other sites

So, I come up today with next example (see .gif below) which for now implement almost all I need for multiple download support and non blocking info report every second to ListView column (using AdlibRegister method).

For now this example uses classic "Message Loop" and maybe I would rewrote it to "On Event" mode, when I would get more practical skills.

gui_autoit_downloader.gif.06de188e68c7ceb75538595c1c73a7dc.gif

 

Share this post


Link to post
Share on other sites

Last solution : use the usual Message Loop mode, with loops and so on, and GUIRegisterMsg
Here is a basic example

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

Global $stop = 0, $exit = 0

GUICreate("My GUI", 200, 100) 
$label = GUICtrlCreateLabel("", 10, 30, 50, 20)
$label2 = GUICtrlCreateLabel("I'm gone", 60, 30, 80, 20)
$btn = GUICtrlCreateButton("test", 10, 50, 50, 20)
$btn2 = GUICtrlCreateButton("exit", 120, 50, 50, 20)
GUISetState()  

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

For $i = 1 to 1000
  If $exit Then Exitloop
  GuiCtrlSetData($label, $i)
  Sleep(300)
Next
Msgbox(0,"", "Exit !")

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
 Switch BitAND($wParam, 0x0000FFFF)
    Case $btn
        $stop =  not $stop
    GuiCtrlSetData($label2, $stop ? "Hey, I'm here !" : "I'm gone")  
     Case $btn2
    $exit = 1
EndSwitch
  Return 'GUI_RUNDEFMSG'
EndFunc

 

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...