Continue running program but listen for terminate event

Started by ipeni, Dec 06, 2020, 06:39 AM

Previous topic - Next topic

ipeni

Hello,

I am trying to run a program that listens for a terminate message from the server but continues to run the rest of the program.

the program the computer sends to the turtles looks like this
while true do
    os.startTime(15)
    os.pullEventTime()

    name = drawer.getName()
    count = drawer.getItemCount()
    data = {name, count}

    modem.transmit(20,21,data)
end

the code the turtles use to listen for the command is


local function execute()
    e, s, sC,rC, mes, dist = os.pullEvent("modem_message")
    if message == "restart" then
        execute()
    elseif message == "terminate"
        return
    else
        f = fs.open("./temp.lua", "w")
        f.write(mes)
        f.close()
        shel.run("./temp")
    end
end

the immediate problem that stuck out to me was the fact that the while loop that is sent over is running forever so the turtle will never listen for another event the way Ive got it set up at the moment, but I cant think of how to do this.

any help would be appreciated, thank you

Lupus590

You want os.pullEventRaw() it doesn't act on the terminate event as the normal pull event does, instead, it just passes the event to the caller (your program). A gotcha here is that should a terminate event be pulled it will be passed to the caller even if the caller specified an event filter, i.e. the terminate event ignores the event filter so any usage of pullEventRaw should know how to handle a terminate event as well as it's requested event.

https://tweaked.cc/module/os.html#v:pullEventRaw

QuickMuffin8782

For beginners, here's something you could do with os.pullEvent.
-- make sure program cannot be terminated
oldPullEvent = os.pullEvent --<[ For to return to be able to terminate ]>
os.pullEvent = os.pullEventRaw

-- make the program able to terminate after
os.pullEvent = oldPullEvent
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782