-1

ユーザーが何かを入力しない限り、画面上の時計を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")

残念ながら、それは機能していません。誰かがこれで私を助けることができれば、本当に感謝しています. ありがとう :)

4

2 に答える 2

0

参考までに、Lua には、複数のルーチンを同時に実行する場合のような「マルチスレッド」はありません。それが持っているのは「スレッドパーキング」です。ルーチンを切り替えて (降伏)、元に戻すことができます。中断したところから再開しますが、一度にアクティブになるのは 1 つのルーチンだけです。

これは私の頼りになる Lua リファレンスで、詳細を説明しています: http://lua-users.org/wiki/CoroutinesTutorial

于 2014-10-20T16:30:32.097 に答える