Sign in to follow this  
Followers 0
programit

How to pause ALL scripts from GUI

6 posts in this topic

Hello,

I have a GUI that I use to select which operations to run(via check boxes), and then I click start, and it runs the selected operations. Now I have a non-operational pause button on my GUI that I would like to make functional. I originally tried to add code off of the click of the pause button that runs an infinite loop until the resume button is clicked. BUT when the start button is clicked, it goes off on its own code and the GUI stops reading in values until the processes running off the start button has completed and it returns to the GUI. Which defeats the point.

So. Is there a way to better accomplish this?

Thanks,

Corey

The GUI Code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GuiButton.au3>
#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <ListBoxConstants.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;~ #include "vShip_GeneralFunctions.au3"
#include "TestCaseMain.au3"

Global $hItem[20]

_Main()

Func _Main()
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    
;Build array of tests to be used to create X amount of check boxes
    $query = "SELECT * FROM AutoQA.dbo.TestCases"
    GetDBRow($query,"AutoQA")

    Opt("GUIOnEventMode", 1)
    #Region ### START Koda GUI section ### Form=C:\Documents and Settings\csnyder\Desktop\AutoIT\Main.kxf
    $Form1 = GUICreate("Form1", 633, 447, 190, 124)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
    GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
    GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
    Global $Log = GUICtrlCreateEdit("", 16, 208, 601, 209) 
    $Label1 = GUICtrlCreateLabel("Test Cases:", 16, 8, 60, 17)
    Global $Pause = GUICtrlCreateButton("Pause", 408, 144, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "PauseClick")
    $Start = GUICtrlCreateButton("Start", 512, 144, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "StartClick")
    $Current = GUICtrlCreateLabel("Current Progress", 408, 48, 82, 17)
    $Total = GUICtrlCreateLabel("Total Progress", 408, 96, 72, 17)
    $currentprogress = GUICtrlCreateProgress(408, 64, 198, 17)
    $totalProgress = GUICtrlCreateProgress(408, 112, 198, 17)
    $CurrentTest = GUICtrlCreateLabel("Current Test", 408, 24, 180, 17)
    Global $TreeView1 = GUICtrlCreateTreeView(16, 24, 369, 161,$iStyle, $WS_EX_CLIENTEDGE)
    _GUICtrlTreeView_BeginUpdate($TreeView1)
    $end = 0
    $i = 1
    While $end = 0
        $testCase = $getDBRow[$i][1]
        If $getDBRow[$i][0] == "END" Then ExitLoop
        $hItem[$i] = GUICtrlCreateTreeViewItem(StringFormat($testCase), $TreeView1)
        $i = $i + 1;Increment Array Counter
    WEnd
    _GUICtrlTreeView_EndUpdate($TreeView1)
    
    $MenuItem1 = GUICtrlCreateMenu("&File")
    $MenuItem2 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
    GUICtrlSetOnEvent(-1, "ExitClick")
    GUISetState(@SW_SHOW)
    Dim $Form1_AccelTable[1][2] = [["x", $MenuItem2]]
    GUISetAccelerators($Form1_AccelTable)
    #EndRegion ### END Koda GUI section ###
 
    While 1
        Sleep(100)
    WEnd

EndFunc  ;==>_Main

;************FUNCTIONS********************;
Func ExitClick();File > Exit
    Exit
EndFunc
Func Form1Close();When form is closed via red X or top left menu. 
    Exit
EndFunc
Func Form1Maximize();Runs when form is maximized
EndFunc
Func Form1Minimize();Runs when form is maximized
EndFunc
Func Form1Restore();Runs when form is restored or opened
EndFunc
Func PauseClick()
;~  MsgBox(1,"","PauseClick")
    
    $PauseText = _GUICtrlButton_GetText($Pause)
;~  MsgBox(1,"","Pause Text = " & $PauseText)
    If $PauseText = "Pause" Then
    _GUICtrlButton_SetText($Pause,"Resume") 
    $TempText = _GUICtrlButton_GetText($Pause)
    While($TempText == "Resume")
        sleep(100)
        $ButtonState = _GUICtrlButton_GetState($Pause)
        ConsoleWrite("Button State: " & $ButtonState & @CRLF)
        If $ButtonState == '620' Then ExitLoop
    WEnd
    
    Else
    _GUICtrlButton_SetText($Pause,"Pause")
    EndIf
EndFunc
Func StartClick()
;~  MsgBox(1,"","Start button Clicked")
    
;********* BEGIN SETTING dbo.TestCases ****************
    For $x = 1 To UBound($hItem) - 1
;~      $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $hTreeView)
        If $hItem[$x] == "" Then ExitLoop
        $checked = _GUICtrlTreeView_GetChecked($TreeView1, $hItem[$x])
;~      MsgBox(1,"",$getDBRow[$x][1] & " is set to " & $checked)
        If $checked Then
        $query = "UPDATE AutoQA.dbo.TestCases SET Active = 1 WHERE TestCaseName = '" & $getDBRow[$x][1] & "'"
        Else
        $query = "UPDATE AutoQA.dbo.TestCases SET Active = 0 WHERE TestCaseName = '" & $getDBRow[$x][1] & "'"
        EndIf
        ExecuteQuery_NonQuery($query,"AutoQA")
    Next
;********* END SETTING dbo.TestCases ****************
    
    BeginTests();RUN MAIN
EndFunc
Func AppendLog($tempstring);Add data to the Log Txt area box.
    _GUICtrlEdit_AppendText($Log, @CRLF & $tempstring)  
EndFunc
Func TotalClick()
    MsgBox(1,"","TotalClick")
EndFunc

What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello,

I have a GUI that I use to select which operations to run(via check boxes), and then I click start, and it runs the selected operations. Now I have a non-operational pause button on my GUI that I would like to make functional. I originally tried to add code off of the click of the pause button that runs an infinite loop until the resume button is clicked. BUT when the start button is clicked, it goes off on its own code and the GUI stops reading in values until the processes running off the start button has completed and it returns to the GUI. Which defeats the point.

So. Is there a way to better accomplish this?

Thanks,

Corey

The problem is how you are using the functions called by GUI events. Event handling functions should be very short and fast, i.e. they should change a flag or a global variable's value and return very quickly. This is because events are handled sequentially. The second event's function (Pause) will not interrupt the first event's function (Start).

Here is a working Start/Stop GUI with a Pause/Continue button that works:

#include <GuiConstantsEx.au3>

Global $hGUI, $ctrlLabel, $ctrlStartStop, $ctrlPauseContinue
Global $fRun = False, $fPause = False, $iCount = 0, $iTimer = TimerInit()

Opt("GuiOnEventMode", 1)

$hGUI = GuiCreate("Test", 350, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
$ctrlLabel = GUICtrlCreateLabel("Count = 0", 10, 10, 330, 20)
$ctrlStartStop = GUICtrlCreateButton("START", 50, 150, 100, 30)
GUICtrlSetOnEvent(-1, "_StartStopButton")
$ctrlPauseContinue = GUICtrlCreateButton("PAUSE", 200, 150, 100, 30)
GUICtrlSetOnEvent(-1, "_PauseButton")
GUISetState()

While 1
    If ($fRun = True) And ($fPause = False) And (TimerDiff($iTimer) >= 1000) Then
        $iTimer = TimerInit()
        $iCount += 1
        ControlSetText($hGUI, "", $ctrlLabel, "Count = " & $iCount)
    EndIf
    Sleep(10)
WEnd

Func _Quit()
    Exit
EndFunc

Func _StartStopButton()
    $fRun = Not $fRun
    If $fRun Then
        $iCount = 0
        ControlSetText($hGUI, "", $ctrlLabel, "Count = " & $iCount)
        ControlSetText($hGUI, "", $ctrlStartStop, "STOP")
    Else
        ControlSetText($hGUI, "", $ctrlStartStop, "START")
    EndIf
EndFunc

Func _PauseButton()
    $fPause = Not $fPause
    If $fPause Then
        ControlSetText($hGUI, "", $ctrlPauseContinue, "CONTINUE")
    Else
        ControlSetText($hGUI, "", $ctrlPauseContinue, "PAUSE")
    EndIf
EndFunc

The key here is that the event functions are short and quick -- they just change the flag and few values and return within a couple of milliseconds.

If you put a loop inside the the start or pause function, it will stop working correctly because further events will not be processed until the pause function returns.

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Well, unfortunately this doesn't help me any. Is there a way to be able to click a button to PAUSE every script running? Basically I want to be able to pause in the middle of my tests when I see something happen that is odd. Then I can check the DB and see whats going on, and then continue. Usually in the middle of my tests, I have about 3 AutoIt windows on my task bar running b/c of nested scripts. I suppose I could always click and pause, the furthest out script instead of trying to do it from a GUI.. Thanks anyway.


What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites

Well, unfortunately this doesn't help me any. Is there a way to be able to click a button to PAUSE every script running? Basically I want to be able to pause in the middle of my tests when I see something happen that is odd. Then I can check the DB and see whats going on, and then continue. Usually in the middle of my tests, I have about 3 AutoIt windows on my task bar running b/c of nested scripts. I suppose I could always click and pause, the furthest out script instead of trying to do it from a GUI.. Thanks anyway.

You could save all the PIDs of the daughter processes as you kick them off and then, for example pause/unpause with a DLL call.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

You could save all the PIDs of the daughter processes as you kick them off and then, for example pause/unpause with a DLL call.

:P

I accidentally posted this question on your provided link :(

Well I will give this a try. In the example you gave, what does this part do:

Func _ProcessPauseSwitch($iPIDOrName, $iSuspend = True)

It seems like it accepts a 2nd parameter, and then sets it to 'TRUE'. Please explain.


What I'm using AutoIt for: I was hired as QA to write automated tests for our software. So I macro any possible situation/process a user could get themselves into, and then check to see that the outcome is what I expect.

Share this post


Link to post
Share on other sites

I accidentally posted this question on your provided link :P

Well I will give this a try. In the example you gave, what does this part do:

Func _ProcessPauseSwitch($iPIDOrName, $iSuspend = True)

It seems like it accepts a 2nd parameter, and then sets it to 'TRUE'. Please explain.

The "Func" line declares the function, it doesn't run it when it appears in the code. Because the second parameter has a value assigned, it is therefore optional. If you don't provide the second parameter it will default to 'True'. You could call that function three ways with the same PID:
$iPID = Run("C:\Whatever\Wherever\SomeProgram.exe")

; 1. Pause process (by default)
_ProcessPauseSwitch($iPID)

; 2. Resume process
_ProcessPauseSwitch($iPID, False)

; 3. Pause again (explicit)
_ProcessPauseSwitch($iPID, True)

Read the help file on Func, and the section titled "Function Notes".

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0