Jump to content




Multitasking in same shell menu

lua

1 reply to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

Posted 08 June 2016 - 07:04 AM

​MultiTasking in same panel?

Is it possible?
I have an small example of multitasking which I wonder is possible.
local message = read()

-- Now it normaly would wait for a response for the read function,
-- but is it possible to make it do functions before the function
-- recieve any data? ex.

----------------------------------
local message = read()

-- V This will happen when its waiting for response.
while message = false then
 -- Do something
end
-- In the while statement I guess there would be something else ofcourse, but how?


-- V Will happen when its getting response for read()
if message=="String" then
 -- Do something
end
-- Is it possible?



#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 08 June 2016 - 08:04 AM

The usual way of things is to write an event listener loop which can handle every event type your script is interested in (as opposed to calling a pre-made function such as read(), which can only handle a subset of events). Eg:

while true do
  local event, par1, par2, etc = os.pullEvent()

  if event == "key" or event == "char" then
    add stuff to input string

  elseif event == "rednet_message" then
    process incoming rednet traffic

  etc

However, the parallel API allows you to run multiple functions as coroutines, and allows execution control to switch between them whenever they yield.

parallel.waitForAny(
  function()
    call read and store the result somewhere
  end,

  function()
    handle incoming rednet traffic
  end
)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users