PDA

Показать полную графическую версию : [решено] Как прекратить выполнение функции не ожидая пока она завершится.


HORRIBLE
07-05-2010, 20:05
Что у меня имеется:

;#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() прекращал свое действие?
Видел тут вот (http://www.forum.oszone.ru/thread-173720.html) похожую тему, но как к себе ее прикруть не понимаю.

Creat0R
07-05-2010, 20:56
Несколько вариантов:

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

Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", 1)

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False), $ch_name = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)

....

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Main_Events()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) @GUI_CtrlId (http://www.autoitscript.com/autoit3/docs/macros.htm#@GUI_CtrlId)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $butt_start
$ch_name = Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $ch_name

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $ch_name Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($butt_start,"СТОП")
_calc() ;надо запустить
Else (http://www.autoitscript.com/autoit3/docs/keywords.htm#Else)
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($butt_start,"СТАРТ")
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _calc()
ConsoleWrite (http://www.autoitscript.com/autoit3/docs/functions/ConsoleWrite.htm)("Старт"&@LF (http://www.autoitscript.com/autoit3/docs/macros.htm#@LF))

...

Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", 0)

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) GUIGetMsg (http://www.autoitscript.com/autoit3/docs/functions/GUIGetMsg.htm)()
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $butt_start
$ch_name = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($butt_start, "СТАРТ")

ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)

...
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", 1)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

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

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $ch_name = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)

...

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
Sleep (http://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(10)

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $ch_name Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
_calc() ;надо запустить
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Main_Events()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) @GUI_CtrlId (http://www.autoitscript.com/autoit3/docs/macros.htm#@GUI_CtrlId)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $butt_start
$ch_name = Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $ch_name

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $ch_name Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($butt_start, "СТОП")
ElseIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#ElseIf) GUICtrlRead (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlRead.htm)($butt_start) <> "СТАРТ" Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($butt_start, "СТАРТ")
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

...




© OSzone.net 2001-2012