Sign in to follow this  
Followers 0
Grantson

Exiting a case statement

10 posts in this topic

Hi Guys

I have done a search but have come up blank

so my appologies if this has been asked before

I have a script with a gui and various buttons, one of the buttons installs a printer driver. The script uses a series of winwaitactive statements and controlsend/control click lines to step through the add printer driver wizard.

I am having a poblem that if the printer driver is already installed the printer properties are displayed and my script sticks.

Im looking for a method to exit the case statement (ideally via a hotkey) so that the application does not need to be terminated and relaunched

Thanks

Grant

Share this post


Link to post
Share on other sites



Without code to look at its a shot in the dark.

I imagine the prperty dialogue appears after clicking a certain button, or performing a certain action.

At that point, its a good Idea to check if the dialogue exists before trying to continue.

Something like...

Func _myFunc()
    ClickButton()
    If WinExists("Property Dialogue") Then
       WinClose("Property Dialogue")
       Return
    Else
       ;continue your code
    EndIf
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I think what he means is that he wants a hotkey that will manually terminate the script. I would recommend reading through this tutorial as it seems to be exactly what you are looking for, and I know that the tutorial (like all by Melba23) is very good to learn from.

Share this post


Link to post
Share on other sites

Regarding "Exiting a case statement". Here is one I learned from Valik. 8)

Switch 1
    Case 1
        Do
            ConsoleWrite("1")
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            If True Then ; here we decide we want to Exit
                ExitLoop
            EndIf
            ConsoleWrite("2")
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
            Sleep(10)
        Until 0
        
    Case 2
    
EndSwitch
ConsoleWrite("3" & @CRLF)

Structured goto. And it's not bad.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

My guess is something like this is being used

Switch $var
       Case 1
            ControlClick()
            MouseClick()
            Controlsend()
            ControlClick()
            If $this Then
               $that = $the_other
            Endif
       Case 2
            ;Blah yada ding dong
       Case Else
            ;Whatevr
EndSwitch

as opposed to

Switch $var
       Case 1
            _myfunc()
       Case 2
            ;Blah yada ding dong
       Case Else
            ;Whatevr
EndSwitch

Func _myfunc()
    ControlClick(
    MouseClick()
    ontrolsend()
    ControlClick()
    If $this Then
       $that = $the_other
    Endif
EndFunc

EDIT:

Did not know you could exit a Switch case with exitloop.

I imagine it works with Select also ?

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

While we're shooting in the dark:

It sounds to me like the script gets stuck on a blocking function like WinWaitActive. If this is the case you could set a timeout for that function, or write a function that is functionally identical, but allows to a hotkey to be set like this: (untested)

#include <Misc.au3>

_WinWaitActiveEx( "SomeTitle", "", 30*1000, "23") ;This would wait for a windows with a title starting with "SomeTitle" to become active for a maximum of 30 seconds, or untill the "End" key is pressed by the user.

;Uses the combined parameters of WinWaitActive and _IsPressed.
;The helpfile for these functions applies.
Func _WinWaitActiveEx($vTitle, $sText = "", $iTimeOut = 0, $sHexKey = "", $vDLL = "user32.dll")
    Local $hWnd, $iTimer = TimerInit()
    While 1
        $hWnd = WinActive($vTitle, $sText) ;WinActive is the non-blocking version of WinWaitActive.
        If $hWnd Then Return $hWnd ;if the window was active, the handle is returned like WinWaitActive would do.
        If $iTimeOut And TimerDiff($iTimer) >= $iTimeOut Then Return SetError(1,0,0) ;if the function times out: 0 is returned like WinWaitActive would do and @error is set to 1.
        If $sHexKey And _IsPressed($sHexKey, $vDLL) Then ;check for interrupting keypress
            While _IsPressed($sHexKey, $vDLL) ;wait for the key to be released, before exiting. This way you won't interrupt the next _WinWaitEx by mistake
                Sleep(10)
            WEnd
            Return SetError(2,0,0) ;Return 0 and set @error to 2.
        EndIf
        Sleep(10)
    WEnd
EndFunc

Other WinWait...() functions can easily be replaced using this function as a template. (You just have to replace WinActive with, for instance, WinExists to replace WinWait.)

Share this post


Link to post
Share on other sites

JohnOne,

Did not know you could exit a Switch case with exitloop

You cannot. Look carefully at the code and you will see that it exits a Do...Until loop within the Case structure. ;)

It is still a clever trick and is now in my Snippets folder. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

#8 ·  Posted (edited)

Did not know you could exit a Switch case with exitloop.

I imagine it works with Select also ?

You can't. I put a Do Until around it which only executes once. If you want to exit early, you exit from the loop with exitloop and the program ends at the end of the case. As I said, it's structured goto.

Edit: Melba beat me. I cannot delete posts.

Edited by Manadar

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