面向对象busy 函数
myu同学说了下wait函数 ,我看了下是比较好用。我重新改写原来写的busy 函数
贴上来给大家分享下
busy={
interval=0.1,
timeout=10,
new=function()
local b={}
setmetatable(b,{__index=busy})
return b
end,
}
function busy:Next() -- 接口函数
end
function busy:check()
wait.make(function()
world.Execute("halt;set busy")
local l,w=wait.regexp("^(> |)你现在不忙。$|^(> |)设定环境变量:busy \\= \\\"YES\\\"$",self.timeout) --超时
if l==nil then
print "网络太慢或是发生了一个非预期的错误"
self:check()
return
end
if string.find(l,"你现在不忙") then
self:Next()
return
end
if string.find(l,"设定环境变量:busy") then
local f=function() self:check() end--
f_wait(f,self.interval)--这里用到我自己写的函数 ,不想这么复杂用 DoAfterSpecial
return
end
wait(self.timeout) --timeout
end)
end --f wait 函数
-- 线程表,储存正在暂停的线程
local wait_table = {}
--setmetatable(wait_table, {__mode = "v"}) --weak table
-- 被定时器调用以恢复一个暂停的线程
function wait_timer_resume(name)
--print(table.getn(wait_table))
--print("wait_id: ",name)
thread = wait_table
if thread~=nil then
assert(coroutine.resume (thread))
else
print(name," 不存在")
end -- if
wait_table={}--垃圾回收
end -- function wait_timer_resume
-- 在脚本中调用这个函数来暂停当前的线程
function f_wait(address, seconds)
id = "wait_timer_" .. GetUniqueNumber ()
hours = math.floor(seconds / 3600)
seconds = seconds - (hours * 3600)
minutes = math.floor(seconds / 60)
seconds = seconds - (minutes * 60)
--print("等待",seconds,"s"," ",id)
wait_table =coroutine.create(function()
-- print("执行")
address()
end)
status = AddTimer (id, hours, minutes, seconds, "",
timer_flag.Enabled + timer_flag.OneShot +
timer_flag.Temporary + timer_flag.Replace,
"wait_timer_resume")
assert(status == error_code.eOK, error_desc)
end function test()
local b
b=busy.new()
b.interval=1 --间隔1秒
b.timeout=20 --10秒钟超时
b.Next=function()
Send("say hi")
end
b:check()
end 好东西,收藏。yct4
页:
[1]