返回

云风,你好,看到你写的关于lua的一些文章,正好有个问题请教,我想要在lua中实现一个sleep函数,该函数的功能是挂起lua脚本的运行,但是宿主程序仍然能正常运行;我试了很多种办法,都会把宿主程序也一同挂起,以下是其中一种。请问我的这个想法是否能实现?能给个解决思路吗?

function foo (a) if a>0 then os.execute("ping -n " .. tonumber(a + 1) .. " localhost > NUL") end return coroutine.yield(a)end

co = coroutine.create(function (n) while true do foo(n); end; end)

function sleep(n) coroutine.resume(co, n)end;

function main() print( "main.1" ); sleep( 2 ); print( "main.2" ); sleep( 3 ); print( "main.3" );end;

名字: 自动排版 密码:

回复 | (2581) | rain | 2012-08-25 10:53:00