This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
brainstew96's profile picture

Running multiple functions at the same time

Started by brainstew96, 17 October 2014 - 01:35 AM
brainstew96 #1
Posted 17 October 2014 - 03:35 AM
Hello, I'm working on a project where I have a clock on the screen that will update itself say every 5 seconds unless the user inputs something. This is my code so far

http://pastebin.com/cXS6MJnm

Unfortunately both threads aren't running at the same time, and as a result the clock isn't updating. If anyone can help me I would really appreciate it. Thanks in advance :)/>
valithor #2
Posted 17 October 2014 - 04:16 AM
Your problem might actually be that you only tell thread1 to run once. I assume you meant to put a while true loop or something similar in that function. Also on line 24 you use io.read(). Because of this the program would pause waiting for input, and would not run anything else in that function until something was entered.

I would likely make a separate function solely for updating the time.

function thread3(x,y)
  while true do
	local time = os.time()
	formatedTime = textutils.formatTime(time, false)
	term.setCursorPos(x,y)
	write(formatedTime)
	sleep(5)
  end
end

Also going to point out that on line 42 you forgot a space between local and mytimer, which causes it to name the variable localmytimer. This is the reason that the program continues to run even after 5 seconds pass.
Edited on 17 October 2014 - 02:43 AM
DannySMc #3
Posted 17 October 2014 - 04:08 PM
Your problem might actually be that you only tell thread1 to run once. I assume you meant to put a while true loop or something similar in that function. Also on line 24 you use io.read(). Because of this the program would pause waiting for input, and would not run anything else in that function until something was entered.

I would likely make a separate function solely for updating the time.

function thread3(x,y)
  while true do
	local time = os.time()
	formatedTime = textutils.formatTime(time, false)
	term.setCursorPos(x,y)
	write(formatedTime)
	sleep(5)
  end
end

Also going to point out that on line 42 you forgot a space between local and mytimer, which causes it to name the variable localmytimer. This is the reason that the program continues to run even after 5 seconds pass.

You're best of using os.pullEvent() as this is a much better method, and then use char input to get an option. so press 1 to go to home, press 2 to go to menu, etc.

This can be done like this:

I put the code here as well as the syntax highlighting is better: http://pastebin.com/9eyZZ9wh


-- You're code....
-- Functionise it all so it can be reused. For example:
-- You can have a menu in function menu() -- code end and your different menu options in there own function!]]
-- and now the function:
function help()
-- This can be / do whatever you want!
-- For example show a help menu/screen!
end
function programs()
-- This can be/do whatever you want!
-- For example show the list of programs!
end
function shutdown()
-- This can do / be whatever you want!
-- For example shutdown the computer!
end
function drawTime()
nTime = textutils.formatTime(os.time())
-- print time where ever: example top left corner:
term.setCursorPos(1,1)
print(nTime)
-- start timer for a second to update clock:
os.startTimer(1)
end
function menu()
while true do
  drawTime()
  local args = { os.pullEvent() }
  if args[1] == "timer" then
   drawTime()
  elseif args[1] == "char" then
   if args[2] == "1" then
	-- do something for menu item 1 example go to: the help screen:
	help()
   elseif args[2] == "2" then
	-- do something for menu item 2 example go to: programs:
	programs()
   elseif args[2] == "3" then
	-- do something for menu item 3 example go to: shutdown:
	shutdown()
   end
  end
end
end
-- make sure to the call the menu (or function you're using as the main entry point) function at the bottom, so it runs through all the functions and registers them.
menu()

This can be useful if you want to wait for many things like: monitor_touch, touch screen stuff and even redstone events. Using this allows you to wait for many things, kind of like the parallel wait for any/all :)/>

hope this helps!

Also have a look at the bottom of this page for all possible events that fire using os.pullEvent() if you use mods then there is even more but they won't be displayed here, look at the mod/peripheral documentation for that!
http://computercraft.info/wiki/Os.pullEvent
Edited on 17 October 2014 - 02:13 PM
brainstew96 #4
Posted 17 October 2014 - 08:40 PM
Thank you everyone for you input. I got it working, however I'm curious as to how to stop the mainMenu program from running. Basically, when I move into another program, say memo, it continues to run the clock updater which screws everything up on screen. I'm guessing this is due to the while true loop being used. Is there anyway to essentially stop the mainMenu program? Thanks for all your help, this is looking really cool so far :)/>
brainstew96 #5
Posted 18 October 2014 - 03:10 AM
@DannySMc Thanks man! I got it working with your code :)/>
DannySMc #6
Posted 19 October 2014 - 05:08 PM
@DannySMc Thanks man! I got it working with your code :)/>

Haha glad to be of service, if you need anything else, feel free to PM me, always happy to help :)/>)