Jump to content

interrupt a function


Stephen89
 Share

Recommended Posts

Check out this link for how to interrupt a function.

sorry to make a new thread but i think the title of the former thread is not suited to address my problem so please delete my last post...

thanks alot been tryin to make it work but still got some problem...

below is the function i want to interrupt

i followed the tutorial in the link you gave me but just when i press the hotkey,accelerator or the button, it wont do the function asap. it still waits for this function to finish before interrupted

Func a() ;

$fInterrupt = 0

While 1

$coord = PixelSearch( 168, 644, 191, 670, 0xAA3539, 2 ) ; pixel detect login screen

If Not @error Then

ConsoleWrite("login" & @CRLF)

MouseClick("Left",496,260,1)

sleep(80)

MouseClick("Left",498,485,2)

sleep(80)

ControlSend($prog,'','',$message)

Sleep(100)

MouseClick("Left",453,568,1)

Sleep(200)

ElseIf @error Then

ConsoleWrite("error" & @CRLF)

Sleep(500)

EndIf

If $fInterrupt <> 0 Then

; The flag was set

Switch $fInterrupt

Case 1

ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)

Case 2

ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF)

Case 3

ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF)

EndSwitch

Return

EndIf

Sleep(100)

WEnd

EndFunc

Edited by Stephen89
Link to comment
Share on other sites

  • Moderators

Stephen89,

Not a good idea to start a new thread while the previous one is still open. :graduated:

Now, we need a bit more than a single function to help you - no-one here is going to write all the other code around it to get it to work. So you post some code that shows your main loop and the functions you want to be interrupted and the function you want to do the interrupting and we will take a look. :(

When you post code please use Code tags. Put [autoit ] before and [/autoit ] after your posted code (but omit the trailing space - it is only there so the tags display here). Or press the blue button just under the BOLD toolbar button. :D

See you soon,

M23

P.S. And remove the unwelcome variable names before you post next time - we do notice these things you know! :D

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

 

Link to comment
Share on other sites

Stephen89,

Not a good idea to start a new thread while the previous one is still open. :graduated:

Now, we need a bit more than a single function to help you - no-one here is going to write all the other code around it to get it to work. So you post some code that shows your main loop and the functions you want to be interrupted and the function you want to do the interrupting and we will take a look. :(

When you post code please use Code tags. Put [autoit ] before and [/autoit ] after your posted code (but omit the trailing space - it is only there so the tags display here). Or press the blue button just under the BOLD toolbar button. :D

See you soon,

M23

P.S. And remove the unwelcome variable names before you post next time - we do notice these things you know! :D

ok thanks alot... :D so im still getting a hang of this right now so i will just attach my GUI code plus this function... this ain't my actual code yet i just want to get the hang of it..

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <Misc.au3>

Global $w, $h, $timer, $hFont, $tLayout
$XResolution = @DesktopHeight
$YResolution = @DesktopWidth
$Form1 = GUICreate("888", 151, 224, 0, 0, $WS_POPUP, $WS_EX_LAYERED)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
$bg = "808080"
GUISetBkColor($GUI_BKCOLOR_TRANSPARENT, $Form1)



#region GDI+ init
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($XResolution, $YResolution, $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)

$hBrush = _GDIPlus_BrushCreateSolid()
$hBrush_bg = _GDIPlus_BrushCreateSolid("0xFF" & $bg)

$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Times New Roman")

#endregion
$script = 0
$y = 0
$x = TimerInit()
$1 = 0
$2 = 0
$3 = 0
$prog = "juskupo"
$message = "hello"
$Button1 = GUICtrlCreateButton("Start", 45, 128, 57, 25)
$Button2 = GUICtrlCreateButton("Start", 45, 168, 57, 25)
$Button3 = GUICtrlCreateButton("Pause", 45, 168, 57, 25)
GUICtrlSetState(-1, $GUI_HIDE)
$Label1 = GUICtrlCreateLabel("BOT", 8, 112, 26, 17)
$Label2 = GUICtrlCreateLabel("PVP", 8, 160, 25, 17)
$Label6 = GUICtrlCreateLabel("BOT", 8, 160, 25, 17)
GUICtrlSetState(-1, $GUI_HIDE)
$Label3 = GUICtrlCreateLabel("THE SCRIPT IS RUNNING", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4 = GUICtrlCreateLabel("THE SCRIPT IS PAUSED", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5 = GUICtrlCreateLabel("Hotkeys:" & @CRLF & "'PAUSE BREAK' to toggle PAUSE / RESUME" & @CRLF & "'DEL' to see INFO" & @CRLF & "'END' to exit the program", 8, 32, 150, 70)
GUICtrlSetFont(-1, 5, 500, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label7 = GUICtrlCreateLabel("", 8, 0, 150, 30)
GUICtrlSetColor(-1, 0xFFFFEE)
GUICtrlSetFont(-1, 5, 800, 1, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_HIDE)
$Pic1 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\1.jpg", 24, 75, 97, 65, BitOR($GUI_SS_DEFAULT_PIC,$SS_NOPREFIX,$SS_CENTERIMAGE,$SS_RIGHTJUST,$SS_SUNKEN))
GUICtrlSetState(-1, $GUI_HIDE)
$Pic2 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\2.jpg", 104, 0, 49, 41, BitOR($GUI_SS_DEFAULT_PIC,$SS_NOPREFIX,$SS_CENTERIMAGE,$SS_RIGHTJUST))
GUISetState(@SW_SHOW)

; Set a HotKey
HotKeySet("x", "_Interrupt")

; Declare a flag
$fInterrupt = 0
; Create a dummy control for the Accelerator to action when pressed
$hAccelInterupt = GUICtrlCreateDummy()
; Set an Accelerator key to action the dummy control
Dim $AccelKeys[1][2]=[ ["z", $hAccelInterupt] ]
GUISetAccelerators($AccelKeys)

GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button1
            
            a()
            
        Case $button2
            _Func_2()
    EndSwitch
WEnd


Func a() ; 
$fInterrupt = 0
While 1
$coord = PixelSearch( 168, 644, 191, 670, 0xAA3539, 2 ) 
If Not @error Then
ConsoleWrite("login" & @CRLF)
MouseClick("Left",496,260,1) 
sleep(80)
MouseClick("Left",498,485,2)
sleep(80)
ControlSend($prog,'','',$message)
Sleep(100)
MouseClick("Left",453,568,1)
Sleep(200)


ElseIf @error Then
ConsoleWrite("error" & @CRLF)
Sleep(500)



EndIf
If $fInterrupt <> 0 Then
; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)
Case 2
ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF)
Case 3
ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF)
EndSwitch
Return
EndIf
Sleep(100)
WEnd
EndFunc 


Func _Func_2()
    For $i = 1 To 3
        ConsoleWrite("+Func 2 Running" & @CRLF)
        Sleep(100)
    Next
    ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc

Func _Interrupt()
    ; The HotKey was pressed so set the flag
    $fInterrupt = 2
EndFunc



Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Func 2 button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) =  $button2 Then $fInterrupt = 1
    ; The dummy control was actioned by the Accelerator key so set the flag
    If BitAND($wParam, 0x0000FFFF) =  $hAccelInterupt Then $fInterrupt = 3
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

when i press the first start button , it executes the a() function and because pixel detect will be @error then it will console write "error" when i try to interrupt in this state, it will interrupt. The problem comes in when pixel detect is NOT @error then it executes the command given, while executing the command and i press the interrupt button , it will wait for function a() to stop before _func2_() to execute...

Edited by Stephen89
Link to comment
Share on other sites

  • Moderators

Stephen89,

As it explains in the Wiki tutorial, you need to keep checking within the function if you want to interrupt. You were not doing this and so the function never looked for the flag and therefore never stopped. :(

Just use something like this to check for the flag during the pauses: :D

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

Global $w, $h, $timer, $hFont, $tLayout
$XResolution = @DesktopHeight
$YResolution = @DesktopWidth
$Form1 = GUICreate("888", 151, 224, 0, 0);, $WS_POPUP, $WS_EX_LAYERED)
;GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
;$bg = "808080"
;GUISetBkColor($GUI_BKCOLOR_TRANSPARENT, $Form1)
#cs
#Region GDI+ init
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($XResolution, $YResolution, $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)

$hBrush = _GDIPlus_BrushCreateSolid()
$hBrush_bg = _GDIPlus_BrushCreateSolid("0xFF" & $bg)

$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Times New Roman")

#EndRegion GDI+ init
#ce

$script = 0
$y = 0
$x = TimerInit()
$1 = 0
$2 = 0
$3 = 0
$prog = "juskupo"
$message = "hello"
$Button1 = GUICtrlCreateButton("Start", 45, 128, 57, 25)
$Button2 = GUICtrlCreateButton("Start", 45, 168, 57, 25)
$Button3 = GUICtrlCreateButton("Pause", 45, 168, 57, 25)
GUICtrlSetState(-1, $GUI_HIDE)
$Label1 = GUICtrlCreateLabel("TOB", 8, 112, 26, 17)
$Label2 = GUICtrlCreateLabel("PVP", 8, 160, 25, 17)
$Label6 = GUICtrlCreateLabel("TOB", 8, 160, 25, 17)
GUICtrlSetState(-1, $GUI_HIDE)
$Label3 = GUICtrlCreateLabel("THE SCRIPT IS RUNNING", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4 = GUICtrlCreateLabel("THE SCRIPT IS PAUSED", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5 = GUICtrlCreateLabel("Hotkeys:" & @CRLF & "'PAUSE BREAK' to toggle PAUSE / RESUME" & @CRLF & "'DEL' to see INFO" & @CRLF & "'END' to exit the program", 8, 32, 150, 70)
GUICtrlSetFont(-1, 5, 500, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label7 = GUICtrlCreateLabel("", 8, 0, 150, 30)
GUICtrlSetColor(-1, 0xFFFFEE)
GUICtrlSetFont(-1, 5, 800, 1, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_HIDE)
$Pic1 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\1.jpg", 24, 75, 97, 65, BitOR($GUI_SS_DEFAULT_PIC, $SS_NOPREFIX, $SS_CENTERIMAGE, $SS_RIGHTJUST, $SS_SUNKEN))
GUICtrlSetState(-1, $GUI_HIDE)
$Pic2 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\2.jpg", 104, 0, 49, 41, BitOR($GUI_SS_DEFAULT_PIC, $SS_NOPREFIX, $SS_CENTERIMAGE, $SS_RIGHTJUST))
GUISetState(@SW_SHOW)

; Set a HotKey
HotKeySet("x", "_Interrupt")

; Declare a flag
$fInterrupt = 0
; Create a dummy control for the Accelerator to action when pressed
$hAccelInterupt = GUICtrlCreateDummy()
; Set an Accelerator key to action the dummy control
Dim $AccelKeys[1][2] = [["z", $hAccelInterupt]]
GUISetAccelerators($AccelKeys)

GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            a()
        Case $Button2
            _Func_2()
    EndSwitch
WEnd



Func a() ;
    $fInterrupt = 0
    While 1

        $coord = PixelSearch(168, 644, 191, 670, 0xAA3539, 2)
        ;SetError(0) ; use this to simulate a valid return
        If Not @error Then
            ConsoleWrite("login" & @CRLF)
            ;MouseClick("Left", 496, 260, 1)
            If _CheckDuringPause(80) Then Return
            ;MouseClick("Left", 498, 485, 2)
            If _CheckDuringPause(80) Then Return
            ;ControlSend($prog, '', '', $message)
            If _CheckDuringPause(100) Then Return
            ;MouseClick("Left", 453, 568, 1)
            If _CheckDuringPause(200) Then Return

        ElseIf @error Then
            ConsoleWrite("error" & @CRLF)
            If _CheckDuringPause(500) Then Return

        EndIf

    WEnd
EndFunc   ;==>a

Func _CheckDuringPause($iPause)

    Local $iBegin = TimerInit()
    While TimerDiff($iBegin) < $iPause
        If $fInterrupt <> 0 Then
            ; The flag was set
            Switch $fInterrupt
                Case 1
                    ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)
                Case 2
                    ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF)
                Case 3
                    ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF)
            EndSwitch
            Return 1
        EndIf
        Sleep(10)
    WEnd
    Return 0

EndFunc

Func _Func_2()
    For $i = 1 To 3
        ConsoleWrite("+Func 2 Running" & @CRLF)
        Sleep(100)
    Next
    ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc   ;==>_Func_2

Func _Interrupt()
    ; The HotKey was pressed so set the flag
    $fInterrupt = 2
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Func 2 button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) = $Button2 Then $fInterrupt = 1
    ; The dummy control was actioned by the Accelerator key so set the flag
    If BitAND($wParam, 0x0000FFFF) = $hAccelInterupt Then $fInterrupt = 3
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

I have commented out some of the code so that it runs on my machine - I do not anticipate problems when you uncomment it, but please come back if there are any. You can use the SetError line to simulate the returns from the PixelSearch command.

I also amended two of the label titles - you need to take more care when sanitising your scripts. That makes 2 strikes - do not do it a third time. :graduated:

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

 

Link to comment
Share on other sites

Stephen89,

As it explains in the Wiki tutorial, you need to keep checking within the function if you want to interrupt. You were not doing this and so the function never looked for the flag and therefore never stopped. :(

Just use something like this to check for the flag during the pauses: :D

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

Global $w, $h, $timer, $hFont, $tLayout
$XResolution = @DesktopHeight
$YResolution = @DesktopWidth
$Form1 = GUICreate("888", 151, 224, 0, 0);, $WS_POPUP, $WS_EX_LAYERED)
;GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
;$bg = "808080"
;GUISetBkColor($GUI_BKCOLOR_TRANSPARENT, $Form1)
#cs
#Region GDI+ init
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($XResolution, $YResolution, $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)

$hBrush = _GDIPlus_BrushCreateSolid()
$hBrush_bg = _GDIPlus_BrushCreateSolid("0xFF" & $bg)

$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Times New Roman")

#EndRegion GDI+ init
#ce

$script = 0
$y = 0
$x = TimerInit()
$1 = 0
$2 = 0
$3 = 0
$prog = "juskupo"
$message = "hello"
$Button1 = GUICtrlCreateButton("Start", 45, 128, 57, 25)
$Button2 = GUICtrlCreateButton("Start", 45, 168, 57, 25)
$Button3 = GUICtrlCreateButton("Pause", 45, 168, 57, 25)
GUICtrlSetState(-1, $GUI_HIDE)
$Label1 = GUICtrlCreateLabel("TOB", 8, 112, 26, 17)
$Label2 = GUICtrlCreateLabel("PVP", 8, 160, 25, 17)
$Label6 = GUICtrlCreateLabel("TOB", 8, 160, 25, 17)
GUICtrlSetState(-1, $GUI_HIDE)
$Label3 = GUICtrlCreateLabel("THE SCRIPT IS RUNNING", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4 = GUICtrlCreateLabel("THE SCRIPT IS PAUSED", 8, 32, 136, 57)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetFont(-1, 6, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5 = GUICtrlCreateLabel("Hotkeys:" & @CRLF & "'PAUSE BREAK' to toggle PAUSE / RESUME" & @CRLF & "'DEL' to see INFO" & @CRLF & "'END' to exit the program", 8, 32, 150, 70)
GUICtrlSetFont(-1, 5, 500, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label7 = GUICtrlCreateLabel("", 8, 0, 150, 30)
GUICtrlSetColor(-1, 0xFFFFEE)
GUICtrlSetFont(-1, 5, 800, 1, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_HIDE)
$Pic1 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\1.jpg", 24, 75, 97, 65, BitOR($GUI_SS_DEFAULT_PIC, $SS_NOPREFIX, $SS_CENTERIMAGE, $SS_RIGHTJUST, $SS_SUNKEN))
GUICtrlSetState(-1, $GUI_HIDE)
$Pic2 = GUICtrlCreatePic("D:\Libraries\Desktop\winmacro\2.jpg", 104, 0, 49, 41, BitOR($GUI_SS_DEFAULT_PIC, $SS_NOPREFIX, $SS_CENTERIMAGE, $SS_RIGHTJUST))
GUISetState(@SW_SHOW)

; Set a HotKey
HotKeySet("x", "_Interrupt")

; Declare a flag
$fInterrupt = 0
; Create a dummy control for the Accelerator to action when pressed
$hAccelInterupt = GUICtrlCreateDummy()
; Set an Accelerator key to action the dummy control
Dim $AccelKeys[1][2] = [["z", $hAccelInterupt]]
GUISetAccelerators($AccelKeys)

GUISetState()

; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            a()
        Case $Button2
            _Func_2()
    EndSwitch
WEnd



Func a() ;
    $fInterrupt = 0
    While 1

        $coord = PixelSearch(168, 644, 191, 670, 0xAA3539, 2)
        ;SetError(0) ; use this to simulate a valid return
        If Not @error Then
            ConsoleWrite("login" & @CRLF)
            ;MouseClick("Left", 496, 260, 1)
            If _CheckDuringPause(80) Then Return
            ;MouseClick("Left", 498, 485, 2)
            If _CheckDuringPause(80) Then Return
            ;ControlSend($prog, '', '', $message)
            If _CheckDuringPause(100) Then Return
            ;MouseClick("Left", 453, 568, 1)
            If _CheckDuringPause(200) Then Return

        ElseIf @error Then
            ConsoleWrite("error" & @CRLF)
            If _CheckDuringPause(500) Then Return

        EndIf

    WEnd
EndFunc   ;==>a

Func _CheckDuringPause($iPause)

    Local $iBegin = TimerInit()
    While TimerDiff($iBegin) < $iPause
        If $fInterrupt <> 0 Then
            ; The flag was set
            Switch $fInterrupt
                Case 1
                    ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)
                Case 2
                    ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF)
                Case 3
                    ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF)
            EndSwitch
            Return 1
        EndIf
        Sleep(10)
    WEnd
    Return 0

EndFunc

Func _Func_2()
    For $i = 1 To 3
        ConsoleWrite("+Func 2 Running" & @CRLF)
        Sleep(100)
    Next
    ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc   ;==>_Func_2

Func _Interrupt()
    ; The HotKey was pressed so set the flag
    $fInterrupt = 2
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Func 2 button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) = $Button2 Then $fInterrupt = 1
    ; The dummy control was actioned by the Accelerator key so set the flag
    If BitAND($wParam, 0x0000FFFF) = $hAccelInterupt Then $fInterrupt = 3
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

I have commented out some of the code so that it runs on my machine - I do not anticipate problems when you uncomment it, but please come back if there are any. You can use the SetError line to simulate the returns from the PixelSearch command.

I also amended two of the label titles - you need to take more care when sanitising your scripts. That makes 2 strikes - do not do it a third time. :graduated:

M23

thank you very much, i'll check it right away and comment back if it's working. :D
Link to comment
Share on other sites

i managed to fix it..

i placed this in check while paused then

Case 1
                    ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)
                    $fInterrupt = 0

then changed func a() a bit..

Func a() ;
    $fInterrupt = 0
    While 1

        $coord = PixelSearch(168, 644, 191, 670, 0xAA3539, 2)
        SetError(0) ; use this to simulate a valid return
        If Not @error Then
            ConsoleWrite("login" & @CRLF)
            ;MouseClick("Left", 496, 260, 1)
            If _CheckDuringPause(80) Then TogglePause()
            ;MouseClick("Left", 498, 485, 2)
            If _CheckDuringPause(80) Then TogglePause()
            ;ControlSend($prog, '', '', $message)
            If _CheckDuringPause(100) Then TogglePause()
            ;MouseClick("Left", 453, 568, 1)
            If _CheckDuringPause(200) Then TogglePause()
                ElseIf @error Then
            ConsoleWrite("error" & @CRLF)
            If _CheckDuringPause(500) Then Return

        EndIf

    WEnd
EndFunc   ;==>a

then added

Func TogglePause() ;when you press pause break
    $chooser = 0
    $Paused = NOT $Paused
    While $Paused
        If _CheckDuringPause(100) Then TogglePause()
        ToolTip('Script is "Paused"',0,0)
        
        
    WEnd

    ToolTip("")

EndFunc
to button 2

it's working well now, all thanks to you...

last noobish question, what does return, return 0, return 1 mean? all i know is that return "exits the function"

sorry im new here :graduated:

i've read about this in the help file "Use the Return keyword to exit the function. Unlike built-in functions, user-defined functions return 0 unless another return value is specified.

Note that function declarations cannot appear inside other function declarations." i didnt quite get it can you please explain? i want to use return but everytime i try to use return, it doesn't make any difference with or withoutit.. :(

Edited by Stephen89
Link to comment
Share on other sites

  • Moderators

Stephen89,

what does return, return 0, return 1 mean?

When a built-in function returns, the return value is usually something of interest like an array or the value the function is designed to produce. User Defined Functions (UDFs) will, as the Help file states, return 0 unless you decide otherwise. You can use this return value to return an array or value just like a built-in function or to indicate whether the function was successful. Here is an example: :x

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hLabel = GUICtrlCreateLabel("Testing, testing.....", 10, 10, 280, 20)

$hButton_1 = GUICtrlCreateButton("Test 1", 10,  50, 80, 30)
$hButton_2 = GUICtrlCreateButton("Test 2", 10, 100, 80, 30)
$hButton_3 = GUICtrlCreateButton("Test 3", 10, 150, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            $iResult = Function_1(1, 2)
            MsgBox(0, "Result", $iResult)
        Case $hButton_2
            $iRet = Function_2()
            If $iRet Then
                MsgBox(0, "Function 2", "Success")
            Else
                MsgBox(0, "Function 2", "Failure")
            EndIf
            GUICtrlSetBkColor($hLabel, 0xFFFFFF)
        Case $hButton_3
            $iRet = Function_3()
            MsgBox(0, "Random Number", $iRet)
    EndSwitch

WEnd

Func Function_1($iA, $iB)

    ; This function returns a value - the sum of the 2 parameters
    Return $iA + $iB

EndFunc

Func Function_2()

    ; This built-in function we call here returns a success/failure value, which we repeat
    Return GUICtrlSetBkColor($hLabel, 0xFF0000)

EndFunc

Func Function_3()

    ; Here we just return a random value between 0 and 100
    Return Random(0, 100, 1)

EndFunc

Note that function declarations cannot appear inside other function declarations

In the example above, we could not have written the functions like this:

; This is WRONG!!!

Func Function_1($iA, $iB)

    ; This function returns a value - the sum of the 2 parameters
    Return $iA + $iB

    ; This is what you cannot do <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Func Function_2()

        ; This built-in function we call here returns a success/failure value, which we repeat
        Return GUICtrlSetBkColor($hLabel, 0xFF0000)

    EndFunc

EndFunc

Each function must be declared within its own Func...EndFunc statements - you cannot put another pair inside. :shifty:

All clear? :P

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

 

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