Task:cancel()method
Stops Task execution, and set its status to "terminated".
Return value
This method returns true, or false if the Task was already terminated.
Example
--! luart-extensions
import ui
local win = ui.Window("Task:cancel() sample", 320, 200)
local pb1 = ui.Progressbar(win)
local pb2 = ui.Progressbar(win)
pb1:center()
pb1.y = pb1.y - 20
pb2:center()
pb2.y = pb2.y + 20
pb1.themed = false
pb1.fgcolor = 0xEFB42C
pb2.themed = false
pb2.fgcolor = 0x000050
local btn = ui.Button(win, "Stop the Task")
btn:center()
btn.y = btn.y + 60
local function who_wins()
ui.info(pb1.position >= 100 and "Orange progress bar wins !" or "Blue progress bar wins !")
win:hide()
end
local task = async(function()
while pb1.position < 100 or pb2.position < 100 do
pb1:advance(math.random(1, 10))
pb2:advance(math.random(1, 10))
sleep(500)
end
who_wins()
end)
function btn:onClick()
task:cancel()
who_wins()
end
await win:showasync()