thread sample code
tmid = thread.dispatch( -- dispatch an async task
function()
sys.msleep(2700)
sys.toast("This is the 2.7th second")
end
)
--
tid = thread.dispatch( -- dispatch another async task
function()
sys.msleep(300)
for i=1,10 do
sys.toast("Thread 2: "..i)
sys.msleep(1000)
end
sys.toast("Should not reach here")
end
)
--
-- iPhone 5C pinch-in in Photos example
--
thread.dispatch(function() -- dispatch a swipe task
touch.on(59,165)
:move(297,522)
:msleep(500)
:off()
end)
--
thread.dispatch(function() -- dispatch another swipe task
touch.on(580,1049)
:move(371,1049)
:msleep(500)
:off()
end)
--
proc_queue_clear("message-from-far-away")
eid = thread.register_event( -- register listener for "dictionary has value" event
"message-from-far-away",
function(val)
sys.toast("Received message: "..val)
end
)
--
sys.msleep(300)
thread.wait(tmid)
--
for i=1,10 do
sys.toast("Thread 1: "..i)
sys.msleep(400)
end
--
thread.kill(tid) -- kill Thread 2
thread.unregister_event("message-from-far-away", eid) -- unregister the event listener
--
sys.toast("Done")
Note: uses non-chapter APIs/methods proc_queue_clear
、sys.msleep
、sys.toast
、touch.on
、:move
、:msleep
、:off