Sign in to follow this  
Followers 0
ruslanas402

[Solved] interrupt function Or assign button

6 posts in this topic

#1 ·  Posted (edited)

Hello AutoIt fans, could anyone help  me to get back on track please? :) So I managed to interrupt  my function by pressing "Func two" button. But how can I terminate fuction by pressing "X" in GUI ? How can I assign "X" button to "Func two" if that's possible.

Regards

Ruslanas402

#include <GUIConstantsEx.au3>
 #include <WindowsConstants.au3>
 $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)
 GUISetState()
 
 GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
 Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     If BitAND($wParam, 0x0000FFFF) =  $hButton_2 Then $fInterrupt = 1
     Return $GUI_RUNDEFMSG
  EndFunc
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
         Case $hButton_1
             _Func_1()
         Case $hButton_2
             Exit
     EndSwitch
 WEnd

  Func _Func_1()
    $fInterrupt = 0
    For $i = 1 To 10
        ConsoleWrite("-Func 1 Running" & @CRLF)
        If _Interrupt_Sleep(5000) Then
            Switch $fInterrupt
                Case 1
                    ConsoleWrite("!Func 1 interrrupted" & @CRLF)
            EndSwitch
            Return
        EndIf
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
 EndFunc
 Func _Interrupt_Sleep($iDelay)
    Local $iBegin = TimerInit()
    Do
        Sleep(10)
        If $fInterrupt Then
            Return True
        EndIf
    Until TimerDiff($iBegin) > $iDelay
    Return False
 EndFunc

 

Edited by ruslanas402

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do it like i did in:

;https://autoit.de/index.php/Thread/83656-Button-Klick-erkennen-wenn-Funktion-l%C3%A4uft-so-wie-ein-Hotkey/?postID=669356#post669356
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

TCPStartup()
$gui = GUICreate("KLeines 1 X 1", 400, 130)
$Start = GUICtrlCreateButton("&Start", 10, 10)
$PauseResume = GUICtrlCreateButton("&Pause", 10, 40)
$ResolveIP = GUICtrlCreateButton("&IP", 10, 70)
$Label = GUICtrlCreateLabel("", 100, 70, 280, 22)
$Anzeige = GUICtrlCreateLabel("", 10, 95, 380, 22)
GUISetState()
Global $sIPAddress


While 1
    _EventHandler()
WEnd

Func _EventHandler()
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            TCPShutdown()
            Exit
        Case $Start
            GUICtrlSetState($Start, $GUI_DISABLE)
            For $i = 1 To 10
                For $j = 1 To 10
                    GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
                    _MySleep(500)
                Next
            Next
            GUICtrlSetState($Start, $GUI_ENABLE)
        Case $PauseResume
            If GUICtrlRead($PauseResume) = "&Pause" Then
                GUICtrlSetData($PauseResume, "&Weiter")
                While GUICtrlRead($PauseResume) <> "&Pause"
                    _MySleep(50)
                WEnd
            Else
                GUICtrlSetData($PauseResume, "&Pause")
            EndIf
        Case $ResolveIP
            $tdStart = TimerInit()
            $sIPAddress = TCPNameToIP("www.autoitscript.com")
            If @error Then
                GUICtrlSetData($Label, "Error code: " & @error)
            Else
                GUICtrlSetData($Label, $sIPAddress)
            EndIf
            ConsoleWrite('Time for resolve: ' & TimerDiff($tdStart) & @CRLF)
    EndSwitch
EndFunc   ;==>_EventHandler

Func _MySleep($iMSec)
    Local $iStart = TimerInit()
    Do
        _EventHandler()
    Until TimerDiff($iStart) >= $iMSec
EndFunc   ;==>_MySleep

 

Edited by AutoBert
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

5 minutes ago, AutoBert said:

Read https://www.autoitscript.com/wiki/Interrupting_a_running_function and use the script with

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

 

it must to be some easier solution without hotkeys, I haven't ever seen any program where you have to press special key kombination to determine the program :D already have red it whole day, maybe I still miss something.

Edited by ruslanas402

Share this post


Link to post
Share on other sites

You postet while i was editing, no finished.

Share this post


Link to post
Share on other sites
1 minute ago, AutoBert said:

You postet while i was editing, no finished.

Sorry :)

Share this post


Link to post
Share on other sites

Nice mate, now I'll be able to manage how to apply to my code ! :>

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

  • Similar Content

    • binhnx
      If condition check in a interruptible function. Is it safe?
      By binhnx
      Let start with some examples:
      1. (As seen in another topic: '?do=embed' frameborder='0' data-embedContent>> ).
      Script copied from that topic
      Interrupt function can be any hotkey/event/adlib/callback.
      If-check inside interrupted function/script 
      ; HotKeySet, or any function that can cause script interrupt HotKeySet("{Delete}", "RemoveSelectedControl") ;... Dim $InfoAboutControls[4096] Global $HandleForCurrentControl ;... While 1 $msg = GuiGetMsg() ;.... ;---Code to process moving a control (drag-n-drop)--- ; If check in MAIN LOOP (interruptible function). If $HandleForCurrentControl > 0 Then;Make sure we have a valid control handle ; ***** oops user pressed Delete key and we get pre-empted ; ***** user removed the selected control. ; ***** Returning from the RemoveSelectedControl function and resuming here: GuiCtrlSetPos($InfoAboutControls[$HandleForCurrentControl], $newLeftPosition, $newTopPosition) EndIf WEnd Func RemoveSelectedControl() $HandleForCurrentControl = -1 EndFunc 2. If-check inside interrupt function
      Hotkey only.
      Flag is set right after if check.
      It's possible or not this function print twice or more?
      HotKeySet("{Delete}", "_TestHotkey") Func _TestHotkey() Static $bFlag = True If ($bFlag) Then ; ***** Right here, user pressed Delete key ; ***** bFlag is currently not set ; ***** And the script will execute this function twice or more times, instead of just once $bFlag = False ConsoleWrite("This should be printed only ONCE") EndIf EndFunc 3. Same as 2, but with global flag
      The question is: can I rely on if-checking with a flag, to ensure that my script doesn't do any strange things, as describe in the above examples?
    • johnmcloud
      [SOLVED] Start - Stop loop on the same button
      By johnmcloud
      Hi guys, i have a script with a stop loop like this

      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $fInterrupt = 0 $hGUI = GUICreate("Test", 100, 100) $hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30) $hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30) 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 20 ConsoleWrite("-Func 1 Running" & @CRLF) If $fInterrupt <> 0 Then ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF) 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 _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1 Return $GUI_RUNDEFMSG EndFunc ;==>_WM_COMMAND
      Work fine, but for GUI space problem i need to set it on the same button, one click for start ( label "Start" ) and then the second click for stop it ( label "Stop")

      I can make GUICtrlSetData for change the name, but i don't undestand how to stop the Func without using another button.
      Thanks