Running a timer in background

Started by Jaskowicz, Jan 09, 2021, 12:41 AM

Previous topic - Next topic

Jaskowicz

Heyo, i'm trying to run a timer for 60 seconds in the background so I can still allow monitor_touch input.

I've tried using parallel but I can't seem to get that working. I also tried coroutines but it yields the while true loop until it finishes.

What i'm needing is a timer that counts down for 60 seconds in background so I can update a monitor to show the time left while also doing anything else within the code

Lupus590

#1
What code did you try with parallel that didn't work?

If you want to avoid parallel then something like this would work.

local timerDuration = 60
local timerId = os.startTimer(timerDuration)
while true do
  local event = table.pack(os.pullEvent())
  if event[1] == "monitor_touch" then
    -- process monitor touch event
    -- timerId = os.startTimer(timerDuration) -- if you want to restart the timer after monitor events then here is where you would do it
  elseif event[1] == "timer" and event[2] == timerId then
    -- process timer event
    timerId = os.startTimer(timerDuration) -- restart the timer
  end
end

for a parallel version
local timerDuration = 60
local timerId = os.startTimer(timerDuration)
local function processMonitor()
  while true do
    local event = table.pack(os.pullEvent("monitor_touch"))
    -- process monitor touch event
    -- timerId = os.startTimer(timerDuration) -- if you want to restart the timer after monitor events then here is where you would do it
  end
end

local function processTimer()
  while true do
    local event = table.pack(os.pullEvent("timer"))
    if event[2] == timerId  then
      -- process timer event
      timerId = os.startTimer(timerDuration) -- restart the timer
  end
end

parallel.waitForAny(processMonitor, processTimer)


Jaskowicz

Quote from: Lupus590 on Jan 09, 2021, 01:20 AMWhat code did you try with parallel that didn't work?

If you want to avoid parallel then something like this would work.

local timerDuration = 60
local timerId = os.startTimer(timerDuration)
while true do
  local event = table.pack(os.pullEvent())
  if event[1] == "monitor_touch" then
    -- process monitor touch event
    -- timerId = os.startTimer(timerDuration) -- if you want to restart the timer after monitor events then here is where you would do it
  elseif event[1] == "timer" and event[2] == timerId then
    -- process timer event
    timerId = os.startTimer(timerDuration) -- restart the timer
  end
end

for a parallel version
local timerDuration = 60
local timerId = os.startTimer(timerDuration)
local function processMonitor()
  while true do
    local event = table.pack(os.pullEvent("monitor_touch"))
    -- process monitor touch event
    -- timerId = os.startTimer(timerDuration) -- if you want to restart the timer after monitor events then here is where you would do it
  end
end

local function processTimer()
  while true do
    local event = table.pack(os.pullEvent("timer"))
    if event[2] == timerId  then
      -- process timer event
      timerId = os.startTimer(timerDuration) -- restart the timer
  end
end

parallel.waitForAny(processMonitor, processTimer)


It isn't quite what i'm attempting to accomplish, I'm trying to get a timer to go for 60 seconds but each second will be put on the monitor (so like 10 seconds in the monitor would show "10" rather than 60 seconds after the timer start, it displaying "60".

I've also thought about just making a timer execute for every second and running that timer x amount of time for the seconds needed so i'm gonna go with that and come back if that solution works.

Lupus590

In that case, sleeping for 1 second and then iterating the count on the monitor and sleeping for another second etc. will do what you want.