Jump to content

Still cant understand func interrupt in msgloop


Recommended Posts

So there is example from wiki

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

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

; Declare a flag
$fInterrupt = 0

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

$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)

; 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 $hButton_1
             _Func_1()
         Case $hButton_2
             _Func_2()
     EndSwitch
WEnd

Func _Func_1()
     ; Make sure the flag is cleared
     $fInterrupt = 0
     For $i = 1 To 20
         ConsoleWrite("-Func 1 Running" & @CRLF)
         ; Look for the flag
         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)
     Next
     ConsoleWrite(">Func 1 Ended" & @CRLF)
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) =  $hButton_2 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

And there is for exmpl start and stop buttons. For example when you hit start it starts doing something like clicking mouse, navigating website, refreshing it million times. So I can delete all Consolewrite things here? I already found solution how to stop function but there is too much code and I dont know which I dont need.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
$fInterrupt = 0
$hGUI = GUICreate("Test", 500, 500)
$hButton_1 = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
; I need this part of code?
$hAccelInterupt = GUICtrlCreateDummy()
Dim $AccelKeys[1][2]=[ ["z", $hAccelInterupt] ]
GUISetAccelerators($AccelKeys)
;
GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
         Case $hButton_1
             _Func_1()
         Case $hButton_2
             _Func_2()
     EndSwitch
WEnd
Func _Func_1()
    
     $fInterrupt = 0
     For $i = 1 To 10000              ; I can make while loop here?
        _IEAction($oIE,"refresh")
      
  ; I dont need this right?
         If $fInterrupt <> 0 Then
          
             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)
     Next
     ConsoleWrite(">Func 1 Ended" & @CRLF) ;this too
EndFunc
Func _Func_2()
     For $i = 1 To 2
         ConsoleWrite("+Func 2 Running" & @CRLF) ;and these two?
         Sleep(100)
     Next
     ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc


;;;;i dont need this? I understand only that this thing waits for button click and sends comand to write something in console?
Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $GUI_Button_Stop Then $fInterrupt = 1
     If BitAND($wParam, 0x0000FFFF) =  $hAccelInterupt Then $fInterrupt = 3
     Return $GUI_RUNDEFMSG
 EndFunc   ;==>_WM_COMMAND
Link to post
Share on other sites

Function Interrupt will set $fInterrupt to such a Value(i.e. 2) which can be checked upon to find out how the Function was Closed/Exitted

Since the x button is assigned to the Hotkey, the Function will receive $fInterrupt as 2 when x would be pressed and would exit

The Second was Difficult to Read actually i wasnt getting what u were doin therefore i left that part

and Please Tell what u really and exactly want to do

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to post
Share on other sites

_Func_1() Returns $fInterrupt when func_2 or accelerator or hotkey will get executed

on no interruption _Func_1() returns 0

your simplified code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; Set a HotKey
HotKeySet("x", "_Interrupt")
; Declare a flag
$fInterrupt = 0
$hGUI = GUICreate("Test", 500, 500)
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
; 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()

While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
         Case $hButton_1
    _Func_1()
         Case $hButton_2
    $fInterrupt = 1
   Case $hAccelInterupt
    $fInterrupt = 3
     EndSwitch
WEnd
Func _Func_1()
     ; Make sure the flag is cleared
     $fInterrupt = 0
     For $i = 1 To 20
         If Not $fInterrupt Then Return $fInterrupt
         Sleep(100)
     Next
  Return 0
EndFunc

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

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to post
Share on other sites

Solved. Thanks PhoenixXL for help. Clean code for stopping func :

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

Global $fInterrupt = False

$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Run_Func_1()
    EndSwitch
WEnd

Func _Run_Func_1()
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        Sleep(10)
        If $fInterrupt Then
            $fInterrupt = False
            ConsoleWrite("Stopping Func_1" & @CRLF)
            Return
        EndIf
    WEnd
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Button2 was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) =  $Button2 Then $fInterrupt = True
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND
Link to post
Share on other sites

Why are u registering the message u can already use it in the msgloop, try this code

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $fInterrupt = False
$Form1 = GUICreate("Form1", 263, 168, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 8, 8, 75, 33)
$Button2 = GUICtrlCreateButton("Button2", 96, 9, 75, 31)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Run_Func_1()
        Case $Button2
            $fInterrupt = True
            ;Do Something...
    EndSwitch
WEnd
Func _Run_Func_1()
    ConsoleWrite("Running Func_1" & @CRLF)
    While 1
        Sleep(10)
        If $fInterrupt Then
            $fInterrupt = False
            ConsoleWrite("Stopping Func_1" & @CRLF)
            Return
        EndIf
    WEnd
EndFunc

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to post
Share on other sites
  • Moderators

PhoenixXL,

Have you tried that code you posted? I think not because it does not work - once AutoIt is within a function you cannot use other controls to break into that function directly. :)

Take a look at the Interrupting a running function tutorial in the Wiki to learn more. ;)

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 post
Share on other sites

M23,

it will not actually interrupt the Function but since the function has a while loop in it , the while loop will not be executed again after the respective functions and commands

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to post
Share on other sites
  • Moderators

PhoenixXL,

it will not actually interrupt the Function

So you agree that it does not do what the OP wants then! ;)

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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...