|
楼主 |
发表于 2015-3-2 13:08:37
|
显示全部楼层
呈上 三个函数
-- 线程表,储存正在暂停的线程
local wait_table = {}
--setmetatable(wait_table, {__mode = "k"}) --weak table
-- 被定时器调用以恢复一个暂停的线程
function wait_timer_resume(name)
--print(table.getn(wait_table),"数组大小")
--print("wait_id: ",name)
local thread = wait_table[name]
if thread~=nil and thread~="" then
assert(coroutine.resume (thread),"coroutine exception")
--assert (coroutine.running (), "Must be in coroutine")
else
print(name," 不存在")
end -- if
wait_table[name]=nil
--collectgarbage()--垃圾回收
end -- function wait_timer_resume
-- 在脚本中调用这个函数来暂停当前的线程
function f_wait(address, seconds,ActiveWhenClosed)
if seconds<=0 then
address()
return
end
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[id] =coroutine.create(function()
--print("执行")
address()
end)
if ActiveWhenClosed==true then
status = AddTimer (id, hours, minutes, seconds, "",
timer_flag.Enabled + timer_flag.OneShot +
timer_flag.Temporary + timer_flag.Replace+timer_flag.ActiveWhenClosed,
"wait_timer_resume")
else
status = AddTimer (id, hours, minutes, seconds, "",
timer_flag.Enabled + timer_flag.OneShot +
timer_flag.Temporary + timer_flag.Replace,
"wait_timer_resume")
end
assert(status == error_code.eOK, error_desc[status])
end
function f_clear()
for i,v in pairs(wait_table) do
local thread=wait_table[i]
if thread then
wait_table[i]=nil
--print(i)
--print(coroutine.status(thread))
end
end
wait_table={}
end |
|