Jump to content

Help a noob with a progress bar?


Recommended Posts

A simple noob, I've not ever written any code before. Ever. OK, I wrote one working AutoIt script last week, complete with icon. Oh, and the tutorials...

So I have a script that works - it watches for two services and an .exe to start, then it kicks off Rob Dunns WSUS forced update VBscript. All peachy.

Now I want to get a prgress bar showing the state of the three items being watched. Nothing works, I've been hammering on this for a couple of hours.

I was also looking to change the state and status of the checkboxes, but please don't tell me how to do that. If someone could help me figure out the progress bar, I should be able to use the same help to figure out the checkboxes.

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <ServiceControl.au3>
#include <ProgressConstants.au3>

$noRun = 0
$i = 0
$x = 0

;--------------------------------------------------------------------------------
; DoRun is the main screen and calls other functions
;--------------------------------------------------------------------------------
DoRun()

;--------------------------------------------------------------------------------
; ChkSrv() looks for the services/app to be running
;--------------------------------------------------------------------------------
Func ChkSrv()
    $SlctBox = 0
    While $noRun = 0
        Do
        $i = 0
        If _ServiceRunning("","wuauserv") Then
                $wuauserv = 1
                If _ServiceRunning("","BITS") Then
                    $BITS = 1
                    If ProcessExists("wuauclt.exe") Then
                        $wuauclt = 1
                            $i = $i + $wuauclt + $BITS + $wuauserv
                            call( "Meter", $i )
                            $wuauserv = 0
                            $BITS = 0
                            $wuauclt = 0
                    EndIf
                EndIf
            EndIf
        Until $i = 3
        if $i = 3 Then
        $noRun = 1
    EndIf
    WEnd 
EndFunc

;--------------------------------------------------------------------------------
; Should update the meter but does not! NOOB!
;--------------------------------------------------------------------------------
Func Meter($sMsg)
    if $i = "1" then 
        $x = 33
    EndIf
    if $i = "2" then 
        $x = 66
    EndIf
    if $i = "3" then 
        $x = 100
    EndIf
    GUICtrlSetData ( $Label13, $x )
endfunc

;--------------------------------------------------------------------------------
; Run the tasks with a screen
;--------------------------------------------------------------------------------
Func DoRun()
#Region ### START Koda GUI section ### Form=G:\koda_1.7.0.1\Templates\TestGui.kxf
$WSUSSplash = GUICreate("WSUS Workgroup System Autoupdate", 640, 480, "-1", "-1", "$DS_SETFOREGROUND", "$WS_EX_TOPMOST", "")
$GroupBox1 = GUICtrlCreateGroup("", 2, 0, 629, 452)
$Label1 = GUICtrlCreateLabel("The system will automatically reboot once the WSUS update completes", 72, 32, 505, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Do not interrupt this process! File corruption may result!", 128, 64, 392, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Waiting for services to start: Automatic Updates, BITS, wauaclt.exe", 80, 176, 475, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label7 = GUICtrlCreateLabel("", 296, 256, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label8 = GUICtrlCreateLabel("", 296, 296, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label9 = GUICtrlCreateLabel("", 296, 336, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label10 = GUICtrlCreateCheckbox("Automatic Updates", 264, 256, 161, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$Label11 = GUICtrlCreateCheckbox("BITS", 264, 288, 97, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$Label12 = GUICtrlCreateCheckbox("wuauclt", 264, 320, 97, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$Label13 = GUICtrlCreateProgress(258, 375, 150, 16)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
    While 1
        $msg = GUIGetMsg()
        call ("ChkSrv")
    WEnd
EndFunc

Always carry a towel.

Link to comment
Share on other sites

Edit:Oh, sry

Just change the $i in the Meter function to $sMsg and use Select...Case...EndSelect structure.

Edit2: Also, you can call it instead of using Call("Meter", $i). Meter($i).

Edit3: As to another problem, the Meter function doesn't know about $Label13 because it's declared inside another function. You need to declare this variable as global or outside any function.

Edited by Authenticity
Link to comment
Share on other sites

Authenticity, +5 Karma to you for providing the right kind of advice, and not attempting to fix my code.

None of your advice made sense to me. :o So I figured it out. :( There were also lots of other noob mistakes in the code. :D

But thanks to your hints, now it works! :D

Any and all criticsims, suggestions for improvement, etc. are welcome as I try to teach myself to write code. :P

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         ModemJunki

 Script Function:
    Watches for processes or services to be running, keeps a status on a menu, 
    then does whatever you want (in this case exits with a messagebox)

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <ServiceControl.au3>
#include <ProgressConstants.au3>

Dim $noRun, $wuauserv, $BITS, $wuauclt, $LabelProgress1, $LabelAUPS, $LabelBITS, $LabelWUCL, $mVal

;--------------------------------------------------------------------------------
; DoRun is the main screen and calls other functions
;--------------------------------------------------------------------------------
DoRun()


;********************************** FUNCTIONS ***********************************

;--------------------------------------------------------------------------------
; ChkSrv() looks for the services/app to be running
;--------------------------------------------------------------------------------
Func ChkSrv()
     While $noRun = 0
        Do
        $i = 0
        If _ServiceRunning("","wuauserv") Then
                $wuauserv = 1
        EndIf
        If _ServiceRunning("","BITS") Then
                $BITS = 1
        EndIf
        If ProcessExists("wuauclt.exe") Then
                $wuauclt = 1
        EndIf
; These functions must take place outside the if...then
        $i = $i + $wuauclt + $BITS + $wuauserv
        Meter($i)
        $wuauserv = 0
        $BITS = 0
        $wuauclt = 0
        sleep (1000)
        Until $i = 3
        $noRun = 1
    WEnd
EndFunc

;--------------------------------------------------------------------------------
; Updates the indicators
;--------------------------------------------------------------------------------
Func Meter($sMsg)
    $mVal = 0
    Select
    Case $sMsg = 1
        $mVal = 33
    Case $sMsg = 2
        $mVal = 66
    Case $sMsg = 3
        $mVal = 100
EndSelect
GUICtrlSetData( $LabelProgress1, $mVal )
    If $wuauserv = 1 Then
    GUICtrlSetState( $LabelAUPS, $GUI_ENABLE )
    GUICtrlSetState( $LabelAUPS, $GUI_CHECKED )
    ElseIf $wuauserv = 0 Then
    GUICtrlSetState( $LabelAUPS, $GUI_DISABLE )
    GUICtrlSetState( $LabelAUPS, $GUI_UNCHECKED )
    EndIf
    if $BITS = 1 Then
    GUICtrlSetState( $LabelBITS, $GUI_ENABLE )
    GUICtrlSetState( $LabelBITS, $GUI_CHECKED )
    ElseIf $BITS = 0 Then
    GUICtrlSetState( $LabelBITS, $GUI_DISABLE  )
    GUICtrlSetState( $LabelBITS, $GUI_UNCHECKED )
    EndIf
    if $wuauclt = 1 Then
    GUICtrlSetState( $LabelWUCL, $GUI_ENABLE )
    GUICtrlSetState( $LabelWUCL, $GUI_CHECKED )
    ElseIf $wuauclt = 0 Then
    GUICtrlSetState( $LabelWUCL, $GUI_DISABLE )
    GUICtrlSetState( $LabelWUCL, $GUI_UNCHECKED )
    EndIf
endfunc

;--------------------------------------------------------------------------------
; Run the tasks with a screen
;--------------------------------------------------------------------------------
Func DoRun()
#Region ### START Koda GUI section ### Form=G:\koda_1.7.0.1\Templates\TestGui.kxf
$WSUSSplash = GUICreate("WSUS Workgroup System Autoupdate", 640, 480, "-1", "-1", "$DS_SETFOREGROUND", "$WS_EX_TOPMOST", "")
$GroupBox1 = GUICtrlCreateGroup("", 2, 0, 629, 452)
$Label1 = GUICtrlCreateLabel("The system will automatically reboot once the WSUS update completes", 72, 32, 505, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Do not interrupt this process! File corruption may result!", 128, 64, 392, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Waiting for services to start: Automatic Updates, BITS, wauaclt.exe", 80, 176, 475, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label7 = GUICtrlCreateLabel("", 296, 256, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label8 = GUICtrlCreateLabel("", 296, 296, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label9 = GUICtrlCreateLabel("", 296, 336, 4, 4)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LabelAUPS = GUICtrlCreateCheckbox("Automatic Updates", 264, 256, 161, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$LabelBITS = GUICtrlCreateCheckbox("BITS", 264, 288, 97, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$LabelWUCL = GUICtrlCreateCheckbox("wuauclt", 264, 320, 97, 17)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$LabelProgress1 = GUICtrlCreateProgress(258, 375, 150, 16)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
    While 1
        $msg = GUIGetMsg()
        call ("ChkSrv")
        MsgBox(0, "", "The services and application all were running)
        Exit
    WEnd
EndFunc

Always carry a towel.

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