Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup()
- parameter.action('Run_without_coroutine',bigJob)
- parameter.action('Run_with_coroutine',WithCoroutine)
- end
- function WithCoroutine()
- cos=coroutine.create(bigJobCos)
- coroutine.resume(cos)
- end
- function bigJob()
- output.clear() --clear print output area
- for n=1,10 do
- --time consuming code here
- for j=1,5000000 do local x=3.1^4.5 end
- print('completed',n)
- end
- print('Job done!')
- end
- function bigJobCos()
- output.clear() --clear print output area
- progress=0 --used for printing status of job
- for n=1,10 do
- --time consuming code here
- for j=1,5000000 do local x=3.1^4.5 end
- progress=n
- coroutine.yield()
- end
- progress=-1 --signal end of job
- if cos then cos=nil end --just to be tidy, kill this off
- end
- function draw()
- if progress then --only report if job is running, ie n is not nil
- if progress>0 then
- print('completed',progress) --print progress of job
- coroutine.resume(cos) --carry on calculating
- elseif progress==-1 then
- print('Job done!')
- progress=nil --to stop any more printing
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement