Jump to content

[Solved] interrupt function Or assign button


ruslanas402
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

×
×
  • Create New...