Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
SuperSamir

How do I run background processes?

Question

I have a loop going on in my program that updates data to be displayed. Alongside this loop, I would like to run event.listen("touch"). However, it is either that event.listen never occurs due to it being after the loop, or event.listen interrupting the loop.

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

If event.listen is after a continuous loop then it wont be called until that block is finished. To use event.listen it should be called before the loop starts. Note: the loop will have to yield somehow via either event.pull or os.sleep or computer.pullSignal.

while (some_condition) do
  doSomething(data)
  os.sleep(5)
  doSomethingElse(more, data)
end

event.listen('some_event', listener) -- # this will not run until `some_condition` above returns false or there is an error
event.listen('some_event', listener)

while (some_condition) do
  doSomething(data)
  os.sleep(5)
  -- # if an event named `some_event` happens during this sleep call above then the listener will execute
  doSomethingElse(more, data)
end

background stuff..

local thread = require 'thread'
local event = require 'event'

local bg_proc = thread.create(function()
    print 'starting background thread..'
    while true do
      local ev = event.pull 'key'
      print 'somebody pressed something!'
    end
  end)

print 'starting main loop'
local i = 0
while true do
  i = i + 1
  print('count is ' .. tostring(i))
  os.sleep(1)
end

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.