Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   AutoIt (http://forum.oszone.net/forumdisplay.php?f=103)
-   -   [решено] Как прекратить выполнение функции не ожидая пока она завершится. (http://forum.oszone.net/showthread.php?t=175120)

HORRIBLE 07-05-2010 20:05 1408912

Как прекратить выполнение функции не ожидая пока она завершится.
 
Что у меня имеется:
Код:

;#include "GUIFinder.au3"
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>

Opt("GUIOnEventMode", 1)

Global $iStop = false, $ch_name = true

$hGUI = GUICreate("",250,165,300,300, $WS_SYSMENU,BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

GUICtrlCreateGroup("Win.info", 2, 2, 240, 65)
;$hFinder = GUICtrlCreateFinder(10, 20)
$hParInp_label = GUICtrlCreateLabel("Win.Handle:", 60, 20, 100, 20)
$hHwndInp_label = GUICtrlCreateLabel("Contro.Handle:", 60, 40, 100, 20)
$hHwndInp = GUICtrlCreateInput("", 132, 20, 100, 18)
;$hClassInp = GUICtrlCreateInput("", 40, 62, 100, 20)
$hParInp = GUICtrlCreateInput("", 132, 40, 100, 18)
;$hTitleInp = GUICtrlCreateInput("", 40, 106, 100, 20)

GUICtrlCreateGroup("Изменение параметра:", 2, 70, 188, 70)
$move_from_label        = GUICtrlCreateLabel("От: ", 10, 90, 15, 20)
$move_to_label                = GUICtrlCreateLabel("До: ", 65, 90, 18, 20)
$move_step_label        = GUICtrlCreateLabel("Шаг: ", 122, 90, 22, 20)
$move_sleep_label        = GUICtrlCreateLabel("Задержка: ", 10, 115, 60, 20)
$move_sleep_label_sec        = GUICtrlCreateLabel("сек.", 105, 115, 20, 20)
$move_from_Input        = GUICtrlCreateInput("1", 27, 88, 30, 18)
$move_to_Input                = GUICtrlCreateInput("10", 85, 88, 30, 18)
$move_step_Input        = GUICtrlCreateInput("1", 147, 88, 30, 18)
$move_sleep_Input        = GUICtrlCreateInput("1", 70, 113, 30, 18)

$butt_start = GUICtrlCreateButton("СТАРТ", 190, 75, 50, 40)

GUICtrlSetOnEvent($butt_start, "_Main_Events")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main_Events")

GUISetState()





While 1
    Sleep(10)
        ;If $FINDER_OPEN And (String($FINDER_HWND) <> GUICtrlRead($hHwndInp)) Then
    ;  GUICtrlSetData($hHwndInp, $FINDER_HWND)
      ;  GUICtrlSetData($hParInp, $FINDER_PARENT)
    ;EndIf
WEnd

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case $butt_start
                        If $ch_name Then
                                GUICtrlSetData($butt_start,"СТОП")
                                _calc() ;надо запустить
                                $ch_name = False
                        Else
                                GUICtrlSetData($butt_start,"СТАРТ")
                                $ch_name = True
                        EndIf
    EndSwitch
EndFunc

Func _calc()
        ConsoleWrite("Старт"&@LF)
        Local $from = GUICtrlRead($move_from_Input)
        Local $to        = GUICtrlRead($move_to_Input)
        Local $step = GUICtrlRead($move_step_Input)
        Local $wait = GUICtrlRead($move_sleep_Input)
        Local $plus = false
        ;Local $res        = $from
        Local $starttime = _Timer_Init()

        while 1
                If _Timer_Diff($starttime)/1000 >= $wait Then
                        If        Not $plus then
                                $res = $from
                                $plus = True
                        Else
                                $res = $res + $step
                                if  $res >= $to then
                                        $res = $to
                                        ConsoleWrite("Время: " &  _Timer_Diff($starttime)/1000&@LF)
                                        ConsoleWrite("стоп " & $res &" = "& $to&@LF)
                                        ;_Timer_KillTimer($hGUI,
                                        ExitLoop
                                EndIf
                        EndIf
                ConsoleWrite("Время: " &  _Timer_Diff($starttime)/1000&@LF)
                ConsoleWrite("Результат: " & $res&@LF)
                $starttime = _Timer_Init()
                EndIf
        WEnd
EndFunc

Как сделать чтобы по первому нажатию $butt_start запускался _calc(), а по второму нажатию _calc() прекращал свое действие?
Видел тут вот похожую тему, но как к себе ее прикруть не понимаю.

Creat0R 07-05-2010 20:56 1408943

Несколько вариантов:

1) Отключение режима событии...
Код:

...

Opt("GUIOnEventMode", 1)

Global $iStop = False, $ch_name = False

....

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case $butt_start
            $ch_name
= Not $ch_name

            If $ch_name Then
                GUICtrlSetData($butt_start,"СТОП")
                _calc() ;надо запустить
            Else
                GUICtrlSetData($butt_start,"СТАРТ")
            EndIf
    EndSwitch
EndFunc

Func _calc()
    ConsoleWrite("Старт"&@LF)

    ...


    Opt("GUIOnEventMode", 0)

    While 1
        Switch GUIGetMsg()
            Case $butt_start
                $ch_name
= False
                GUICtrlSetData($butt_start, "СТАРТ")

                ExitLoop
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch

        ...
    WEnd

    Opt("GUIOnEventMode", 1)
EndFunc

2) Проверка и запуск функции в цикле:
Код:

...

Global $ch_name = False

...

While 1
    Sleep(10)

    If $ch_name Then
        _calc() ;надо запустить
    EndIf
WEnd

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case $butt_start
            $ch_name
= Not $ch_name

            If $ch_name Then
                GUICtrlSetData($butt_start, "СТОП")
            ElseIf GUICtrlRead($butt_start) <> "СТАРТ" Then
                GUICtrlSetData($butt_start, "СТАРТ")
            EndIf
    EndSwitch
EndFunc

...



Время: 10:03.

Время: 10:03.
© OSzone.net 2001-