电脑工艺中的多线程函数
2014-10-17 2:46:14
收藏:0
阅读:98
评论:2
我正在进行一个项目,想要更新屏幕上的时钟,每隔5秒钟刷新一次,除非用户输入。这是我目前的代码,
function thread1()
term.clear()
term.setCursorPos(1,1)
write (" SteveCell ")
local time = os.time()
local formatTime = textutils.formatTime(time, false)
write (formatTime)
print ("")
print ("")
for i=1,13 do
write ("-")
end
print("")
print ("1. Clock")
print ("2. Calender")
print ("3. Memo")
print ("4. Shutdown")
for i=1,13 do
write ("-")
end
print ("")
print ("")
write ("Choose an option: ")
local choice = io.read()
local choiceValid = false
if (choice == "1") then
-- do this
elseif (choice == "2") then
-- do that
elseif (choice == "3") then
-- do this
elseif (choice == "4") then
shell.run("shutdown")
else
print ("Choice Invalid")
os.sleep(2)
shell.run("mainMenu")
end
end
function thread2()
local myTimer = os.startTimer(5)
while true do
local event,timerID = os.pullEvent("timer")
if timerID == myTimer then break end
end
return
end
parallel.waitForAny(thread1, thread2)
shell.run("mainMenu")
很不幸,它没有起作用。如果有人能帮我解决这个问题,我将非常感激。谢谢 :)
点赞
用户4162169
FYI,Lua 并没有像同时执行多个例程的 'multi-threading'。它实现的是 'thread parking'。你可以在不同的例程之间进行切换 (yielding),然后再切换回来,它会从离开的地方继续执行,但是任何时候只有一个例程是活跃的。
这是我经常用来参考 Lua 的资料,在这里详细说明了: http://lua-users.org/wiki/CoroutinesTutorial
2014-10-20 16:30:32
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你想要做类似这样的事情(我没有进行正确的屏幕绘制,只是时间)
local function thread1_2() -- 两个线程合在一起! while true do local ID_MAIN = os.startTimer(5) local ID = os.startTimer(0.05) local e = { os.pullEvent() } if e[1] == "char" then -- 检查这里所有带有变量 e [2] 的选项 print(string.format(“已按下%s”,e[2])) 打破--走出 'thread' elseif e[1] ==“timer”并且e [2] == ID then ID = os.startTimer(0.05) - 在cc中最短的间隔 redrawTime() - 在此函数中重绘和更新时间! elseif e[1] ==“timer”并且e [2] == MAIN_ID then 打破 结束 end end另外,在正确的[论坛](http://www.computercraft.info/forums2/index.php ?/forum/14-ask-a-pro/)中提问,那里有更多的机会得到答案!另一个注意事项是更深入地了解事件处理,这真的有帮助。