两个自定义函数同时运行问题
比如if.....then
;;;
else
AdlibRegister("Timer",50)
AdlibRegister("play",500)
endif
同时调用两个自定义函数,timer函数至少需要每1秒调用一次(就是记录时间用的)
play函数每次执行的时间时由变量控制的,至少10秒
所以我每次执行的时候,假如我play函数执行完需要20秒,那timer函数就要等play执行完才继续执行
这样记录时间的时候就不是一秒一秒的跳了,而是21秒一跳
所以我这样做,不能让两个函数同时单独执行,我才入行AU3没几天,脑子里的词很少
想不到有什么功能可用的,麻烦论坛里的大侠们出手帮帮我吧!
只要效果能做到就行,不一定按我这样做! #AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <Timers.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
Opt("MustDeclareVars", 1)
Global $iMemo, $hStatusBar, $progress, $percent = 0, $direction = 1
_Example_CallBack()
Func _Example_CallBack()
Local $hGUI, $iTimerProgress, $btn_change, $iWait = 10, $btn_state
Local $aParts =
$hGUI = GUICreate("Timers Using CallBack Function(s)", 400, 320)
$iMemo = GUICtrlCreateEdit("", 2, 32, 396, 226, BitOR($WS_HSCROLL, $WS_VSCROLL))
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
$btn_state = GUICtrlCreateButton("Start Progress Bar", 70, 270, 100, 25)
$btn_change = GUICtrlCreateButton("Change", 215, 270, 90, 25)
GUICtrlSetState($btn_change, $GUI_DISABLE)
$hStatusBar = _GUICtrlStatusBar_Create($hGUI, $aParts)
_GUICtrlStatusBar_SetText($hStatusBar, "Timers")
_GUICtrlStatusBar_SetText($hStatusBar, @TAB & @TAB & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC), 2)
$progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_SMOOTH)
GUICtrlSetColor($progress, 0xff0000)
_GUICtrlStatusBar_EmbedControl($hStatusBar, 1, GUICtrlGetHandle($progress))
GUISetState()
_Timer_SetTimer($hGUI, 1000, "_UpdateStatusBarClock") ; create timer
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $btn_state
If GUICtrlRead($btn_state) = "Start Progress Bar" Then
$iTimerProgress = _Timer_SetTimer($hGUI, $iWait, "_UpdateProgressBar") ; create timer
If @error Or $iTimerProgress = 0 Then ContinueLoop
GUICtrlSetData($btn_state, "Stop Progress Bar")
GUICtrlSetState($btn_change, $GUI_ENABLE)
Else
GUICtrlSetState($btn_change, $GUI_DISABLE)
_Timer_KillTimer($hGUI, $iTimerProgress)
GUICtrlSetData($btn_state, "Start Progress Bar")
EndIf
Case $btn_change
If $iWait = 10 Then
$iWait = 250
Else
$iWait = 10
EndIf
MemoWrite("Timer for _UpdateProgressBar set at: " & $iWait & " milliseconds")
$iTimerProgress = _Timer_SetTimer($hGUI, $iWait, "", $iTimerProgress) ; reuse timer with different interval
EndSwitch
WEnd
_Timer_KillAllTimers($hGUI)
GUIDelete()
EndFunc ;==>_Example_CallBack
; call back function
Func _UpdateStatusBarClock($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
_GUICtrlStatusBar_SetText($hStatusBar, @TAB & @TAB & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC), 2)
EndFunc ;==>_UpdateStatusBarClock
; call back function
Func _UpdateProgressBar($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
$percent += 5 * $direction
GUICtrlSetData($progress, $percent)
If $percent = 100 Or $percent = 0 Then $direction *= -1
If $percent = 100 Then
GUICtrlSetColor($progress, 0xff0000)
ElseIf $percent = 0 Then
GUICtrlSetColor($progress, 0x0000ff)
EndIf
EndFunc ;==>_UpdateProgressBar
; Write a line to the memo control
Func MemoWrite($sMessage)
GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc ;==>MemoWrite 回复 2# 3mile
这个不是我想要的结果啊,你这里的时间是跟系统时间同步的,我想要的是,当我按下“开始”按钮时,
循环执行一个自定义函数,而就在按下“开始”按钮的同时,开始计时,当按下“停止”按钮时,自定义函数停止,
时间计时也停止,而显示运行了多久! 回复 3# riverboat2
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\My Documents\aaa.kxf
$Form1 = GUICreate("Form1", 438, 211, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Label1 = GUICtrlCreateLabel("", 16, 36, 164, 28)
GUICtrlSetOnEvent(-1, "Label1Click")
$Button1 = GUICtrlCreateButton("开始任务", 216, 32, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("取消任务", 216, 80, 75, 25)
GUICtrlSetOnEvent(-1, "Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $t, $timer
While 1
Sleep(100)
WEnd
Func Button1Click()
$timer = TimerInit()
$t = 20
AdlibRegister("set", 1000)
EndFunc ;==>Button1Click
Func Button2Click()
AdlibUnRegister("set")
MsgBox(4096, "用户取消", "用时" & TimerDiff($timer) & " 毫秒")
EndFunc ;==>Button2Click
Func set()
$t -= 1
GUICtrlSetData($Label1, $t & "秒后重新启动计算机")
If $t < 1 Then
Run(@ComSpec & " /c " & 'shutdown -i', "", @SW_HIDE)
EndIf
EndFunc ;==>set
Func Form1Close()
Exit
EndFunc ;==>Form1Close
Func Form1Maximize()
EndFunc ;==>Form1Maximize
Func Form1Minimize()
EndFunc ;==>Form1Minimize
Func Form1Restore()
EndFunc ;==>Form1Restore
Func Label1Click()
EndFunc ;==>Label1Click 本帖最后由 riverboat2 于 2010-9-18 13:24 编辑
回复 4# 3mile
谢谢你3M,你这里最后统计运行时间的时候是直接统计出来,而不是一秒一秒的计时
这样吧,我写个例子,你看下运行时间是不是每10秒才变化一次,如果我设置延时20秒的话
就是20秒变化一次,如果可以既统计运行次数,又能及时记录运行时间,我想就已经达到我想要的了
(再多问一句,为什么我回复的代码,不带高亮显示的??难道要加参数?)#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\例子.au3
$Form1 = GUICreate("例子",160,100)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Label1 = GUICtrlCreateLabel("已经运行0次",8,10,90,25)
$Label2 = GUICtrlCreateLabel("运行时间: 00时00分00秒",8,50,90,50)
$Button1 = GUICtrlCreateButton("开始",100,10,50,25)
GUICtrlSetOnEvent(-1,"Button1Click")
$Button2 = GUICtrlCreateButton("停止",100,50,50,25)
GUICtrlSetOnEvent(-1,"Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $Hour, $Mins, $Secs , $timer , $t = 0 ,$Time
While 1
Sleep(500)
WEnd
Func Button1Click()
$timer = TimerInit()
AdlibRegister("Timer",50)
AdlibRegister("set",500)
EndFunc
Func Button2Click()
AdlibUnRegister("set")
EndFunc
Func set()
$t += 1
GUICtrlSetData($Label1, "已经运行"&$t&"次")
Sleep(10000) ;之所以这里加上延时,是因为我需要的那个程序这里需要停顿一下,具体时间由变量控制,这里举个例子,停10秒
EndFunc
Func Timer();计时运算函数
_TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
Local $sTime = $Time
$Time = StringFormat("%02i时%02i分%02i秒", $Hour, $Mins, $Secs)
If $sTime <> $Time Then GUICtrlSetData($Label2, "运行时间: "&$Time)
EndFunc
Func Form1Close()
Exit
EndFunc
Func Form1Minimize()
EndFunc
Func Form1Maximize()
EndFunc
Func Form1Restore()
EndFunc 要的是这个效果?如果早跟个样本的话,也不会这么麻烦了
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\例子.au3
$Form1 = GUICreate("例子", 160, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Label1 = GUICtrlCreateLabel("已经运行0次", 8, 10, 90, 25)
$Label2 = GUICtrlCreateLabel("运行时间: 00时00分00秒", 8, 50, 90, 50)
$Button1 = GUICtrlCreateButton("开始", 100, 10, 50, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("停止", 100, 50, 50, 25)
GUICtrlSetOnEvent(-1, "Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $Hour, $Mins, $Secs, $timer, $t = 0, $Time
While 1
Sleep(500)
WEnd
Func Button1Click()
$timer = TimerInit()
AdlibRegister("Timer", 1000)
set()
AdlibRegister("set", 10000)
EndFunc ;==>Button1Click
Func Button2Click()
AdlibUnRegister("set")
EndFunc ;==>Button2Click
Func set()
$t += 1
GUICtrlSetData($Label1, "已经运行" & $t & "次")
;Sleep(10000) ;之所以这里加上延时,是因为我需要的那个程序这里需要停顿一下,具体时间由变量控制,这里举个例子,停10秒
EndFunc ;==>set
Func Timer();计时运算函数
GUICtrlSetData($Label2, "运行时间: " & Int(TimerDiff($timer) / 1000))
;_TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
;Local $sTime = $Time
;$Time = StringFormat("%02i时%02i分%02i秒", $Hour, $Mins, $Secs)
;If $sTime <> $Time Then GUICtrlSetData($Label2, "运行时间: "&$Time)
EndFunc ;==>Timer
Func Form1Close()
Exit
EndFunc ;==>Form1Close
Func Form1Minimize()
EndFunc ;==>Form1Minimize
Func Form1Maximize()
EndFunc ;==>Form1Maximize
Func Form1Restore()
EndFunc ;==>Form1Restore 本帖最后由 riverboat2 于 2010-9-18 17:16 编辑
回复 6# 3mile
呵呵,我才学5天,怕代码写错了,让你们不能明白。
你这个效果是有了,学习了!
很不好意思3M,我举的例子,你这个成功了,但是对我实际情况这个还不行
没想到你把sleep时间变成AdlibRegister的执行等待的时间,呵呵,失误啊!
虽然效果是一样,我在sleep(10000)后面加了注释的,这里的sleep是不能取消的,
我需要做的,这里必须要停顿,如果只停止一次,
那我倒是可以学习你的方法,把时间加在AdlibRegister("set", $time0)中
但是实际上,我这里set()中需要分开停止两次
我把例子再改一下,你看看,例子有点傻,见笑了!#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Date.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\例子.au3
$Form1 = GUICreate("例子",160,100)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Label1 = GUICtrlCreateLabel("已经运行0次",8,10,90,25)
GUICtrlSetColor(-1,0xFF0000)
$Label2 = GUICtrlCreateLabel("运行时间: 00时00分00秒",8,50,90,50)
$Button1 = GUICtrlCreateButton("开始",100,10,50,25)
GUICtrlSetOnEvent(-1,"Button1Click")
$Button2 = GUICtrlCreateButton("停止",100,50,50,25)
GUICtrlSetOnEvent(-1,"Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $Hour, $Mins, $Secs , $timer , $t = 0 ,$Time
While 1
Sleep(500)
WEnd
Func Button1Click()
$timer = TimerInit()
AdlibRegister("Timer",50)
AdlibRegister("set",50)
EndFunc
Func Button2Click()
AdlibUnRegister("set")
EndFunc
Func set()
$t += 1
Sleep(5000) ;之所以这里加上延时,是因为我需要的那个程序这里需要停顿一下,具体时间由变量控制,这里举个例子,停10秒
GUICtrlSetColor($Label1,0xFFFF00)
Sleep(6000)
GUICtrlSetColor($Label1,0xFF0000)
GUICtrlSetData($Label1, "已经运行"&$t&"次")
EndFunc
Func Timer();计时运算函数
_TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs)
Local $sTime = $Time
$Time = StringFormat("%02i时%02i分%02i秒", $Hour, $Mins, $Secs)
If $sTime <> $Time Then GUICtrlSetData($Label2, "运行时间: "&$Time)
EndFunc
Func Form1Close()
Exit
EndFunc
Func Form1Minimize()
EndFunc
Func Form1Maximize()
EndFunc
Func Form1Restore()
EndFunc 回复 6# 3mile
3M 那个问题被关闭了,也好,这个你可以继续帮我实现么(上面已经写明了)?等待回复...... 回复 8# riverboat2
sorry,无法理解你的意思。 回复 9# 3mile
额,我是不是很烦人啊?
第一、set()里我需要Sleep 延时两次,时间变量控制
第二、按开始的时候,开始循环运行set()
同时,开始显示计时
这么说可以理解我么? 不搞懂你意思 AdlibRegister什么时候可以两个连着用了 回复 12# netegg
在我注册的时候就可以了。
不过阻塞的问题不解决的话,还是很恼人。 回复 13# republican
是啊~不能同时单独运行呢~ 两个人的代码大战
页:
[1]
2