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

Need help with Parallel API

Started by Wilma456, 09 June 2016 - 02:00 PM
Wilma456 #1
Posted 09 June 2016 - 04:00 PM
If I use this code, is doesent works. How can I make it works? I want to do thinks while the compiuter wait for an signal.

function Empfang()
  a,b,c,d,e = os.pullEvent("modem_message")
end
function Senden()
  modem.transmit(2,2,"Test")
end
function start()
  parallel.waitForAny(Empfang,Senden)
  start()
end
modem = peripheral.wrap("right")
modem.open(2)
start()
Dog #2
Posted 09 June 2016 - 04:11 PM
Well as your code is structured now, it'll start both Empfang() and Senden(), then Senden() will transmit and exit which will allow parallel.waitForAny to exit as well. You could use parallelWaitForAll, but that still wouldn't change the fact that Senden() would quit after one send and Empfang() would quit after receiving one message. Also your start() function has a recursion problem as it refers to itself - this will cause the program to crash eventually. What you were looking for was an infinite loop of some sort, like this…

function Empfang()
  while true do --# start an infinite loop
    a, b, c, d, e = os.pullEvent("modem_message") --# os.pullEvent yields (allowing other computers to run), so this loop is 'safe'
  end
end

function Senden()
  while true do --# start an infinite loop
    modem.transmit(2, 2, "Test")
    sleep(1) --# if you don't sleep (or yield in some way in a loop) it will crash for failure to yield
  end
end

modem = peripheral.wrap("right")
modem.open(2)
parallel.waitForAny(Empfang, Senden)
Edited on 09 June 2016 - 02:14 PM
Wilma456 #3
Posted 09 June 2016 - 05:18 PM
Thankyou