I do create a Task system with several features like :
Lua Code:
Task.DirectCall(func, ...) -- Call the method as soon as possible(call it directly or next phase if the operation time is fully used)
Task.NextCall(func, ...) -- Call in next phase(next OnUpdate)
Task.DelayCall(delay, func, ...) -- Call the func with a delay, just like the C_Timer.After
Task.EventCall(event, func) -- Call the func when the event is fired, no args since the event would provide
Task.NoCombatCall(func, ...) -- Call the func when the player is not in combat, used for secure frames.
Task.WaitCall([delay, ][event, ...,] func) -- Call the func when meet the delay or any event is fired.
Task.ThreadCall(func, ...) -- Call the func in a thread from the thread pool.
Also several features for thread only
Lua Code:
Task.Continue() -- Check if the current thread should keep running or wait for next time
Task.Next() -- Make the current thread wait for next phase
Task.Delay(delay) -- Delay the current thread for several second
Task.Event(event) -- Make the current thread wait for a system event, the args would be returned
Task.Wait([delay, ][event, ... ,]) -- Make the current thread wait for a delay or any of the events,if it's an event, the event and it's args would be returned.
For a simple container refresh, the code would be like
Lua Code:
local toggle = true
Task.ThreadCall(function()
while toggle do
-- Make sure the operation only process when not in combat
if InCombatLockdown() then Task.Event("PLAYER_REGEN_ENABLED") end
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
-- do something
-- To smooth the updating
Task.Continue()
end
end
-- Wait for next update
Task.Event("BAG_UPDATE_DELAYED")
end
end)
It'd be useful to put all codes together in some conditions.