Lua 计算机工艺中的多线程功能

Lua 计算机工艺中的多线程功能,lua,computercraft,Lua,Computercraft,我正在做一个项目,我想更新屏幕上的时钟,比如说每5秒更新一次,除非用户输入什么。这是我目前掌握的密码 function thread1() term.clear() term.setCursorPos(1,1) write (" SteveCell ") local time = os.time() local formatTime = textutils.formatTime(time, false) write (formatTime) print

我正在做一个项目,我想更新屏幕上的时钟,比如说每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()
  localmyTimer = 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")

不幸的是,它不起作用。如果有人能帮我,我会非常感激。谢谢:)

你想做这样的事情(我没有做正确的屏幕绘图,只是时间)

另外,用适当的方式问这个问题,你有更多的机会得到答案!
另一个注意事项是,深入了解事件处理,它确实很有帮助。

仅供参考,Lua没有同时执行多个例程的“多线程”。它的功能是“线程驻车”。您可以在例程之间切换(让步)并切换回,它将在停止的位置恢复,但在任何给定时间只有一个例程处于活动状态

这是我的go to Lua参考资料,详细说明:

什么是“不工作”?
local function thread1_2()
   -- both threads in one!

   while true do
      local ID_MAIN = os.startTimer(5)
      local ID = os.startTimer(0.05)
      local e = { os.pullEvent() }
      if e[1] == "char" then
         -- Check all the options with variable e[2] here
         print( string.format( "Pressed %s", e[2] ) )
         break -- Getting out of the 'thread'
      elseif e[1] == "timer" and e[2] == ID then
         ID = os.startTimer(0.05) -- shortest interval in cc
         redrawTime() -- Redraw and update the time in this function!!!
      elseif e[1] == "timer" and e[2] == MAIN_ID then
         break
      end
   end
end