大佬教程收集整理的这篇文章主要介绍了最简单的方法让lua脚本等待/暂停/睡眠/阻塞几秒钟?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
> sleep – 停止线程上的所有操作
>暂停/等待 – 不要去下一个
命令,但允许其他代码
应用程序继续
> block – 不要去下一个命令,直到
当前一个返回
我读过一个
while os.clock()<time_point do --nothing end
占用CPU时间。
有什么建议么?有没有一个API调用我错过了?
更新:我写了这个问题很久以前试图让WOW Lua按照时间表重播行动(即站立,等待1秒,跳舞,等待2秒,坐。没有停顿,这些发生几乎所有在同一个季度。 )As it turned out WOW had purposely disabled pretty much everything that allows doing action on a clock because it could break the game or enable bots.我想要重新创建一个时钟,一旦它被带走,我不得不做一些疯狂,像创建一个工作数组(一个动作和执行时间),然后注册一个事件处理程序上一堆常见的事件,像鼠标移动,然后在偶数处理程序中,处理任何时间到来的动作。事件处理程序实际上不会每X毫秒发生一次,但如果它每2-100毫秒发生一次,那就足够了。可悲的是我从来没有尝试过。
-- we "pcall" (try/catch) the "ex",which had better include os.sleep -- it may be a part of the standard library in future Lua versions (past 5.2) local ok,ex = pcall(require,"ex") if ok then -- print("Ex") -- we need a hack now too? ex.install(),you say? okay pcall(ex.install) -- let's try something else. why not? if ex.sleep and not os.sleep then os.sleep = ex.sleep end end if not os.sleep then -- we make os.sleep -- first by trying ffi,which is part of LuaJIT,which lets us write C code local ok,ffi = pcall(require,"ffi") if ok then -- print("FFI") -- we can use FFI -- let's just check one more time to make sure we still don't have os.sleep if not os.sleep then -- okay,here is our custom C sleep code: ffi.cdef[[ void Sleep(int ms); int poll(struct pollfd *fds,unsigned long nfds,int timeout); ]] if ffi.os == "Windows" then os.sleep = function(seC) ffi.C.Sleep(sec*1000) end else os.sleep = function(seC) ffi.C.poll(nil,sec*1000) end end end else -- if we can't use FFI,we try LuaSocket,which is just called "socket" -- I'm 99.99999999% sure of that local ok,socket = pcall(require,"socket") -- ...but I'm not 100% sure of that if not ok then local ok,"luasocket") end -- so if we're really using socket... if ok then -- print("Socket") -- we might as well confirm there still is no os.sleep if not os.sleep then -- our custom socket.SELEct to os.sleep code: os.sleep = function(seC) socket.SELEct(nil,nil,seC) end end else -- now we're going to test "alien" local ok,alien = pcall(require,"alien") if ok then -- print("Alien") -- beam me up... if not os.sleep then -- if we still don't have os.sleep,that is -- now,I don't know what the Hell the following code does if alien.platform == "windows" then kernel32 = alien.load("kernel32.dll") local slep = kernel32.Sleep slep:types{ret="void",abi="stdcall","uint"} os.sleep = function(seC) slep(sec*1000) end else local pol = alien.default.poll pol:types('struct','unsigned long','int') os.sleep = function(seC) pol(nil,sec*1000) end end end elseif package.config:match("^\\") then -- print("busywait") -- if the computer is politically opposed to NIXon,we do the busywait -- and shake it all about os.sleep = function(seC) local timr = os.time() repeat until os.time() > timr + sec end else -- print("NIX") -- or we get NIXed os.sleep = function(seC) os.execute("sleep " .. seC) end end end end end
以上是大佬教程为你收集整理的最简单的方法让lua脚本等待/暂停/睡眠/阻塞几秒钟?全部内容,希望文章能够帮你解决最简单的方法让lua脚本等待/暂停/睡眠/阻塞几秒钟?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。