Advertisement
Guest User

Post9

a guest
Mar 26th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. function setup()
  2. parameter.action('Run_without_coroutine',bigJob)
  3. parameter.action('Run_with_coroutine',WithCoroutine)
  4. end
  5.  
  6. function WithCoroutine()
  7. cos=coroutine.create(bigJobCos)
  8. coroutine.resume(cos)
  9. end
  10.  
  11. function bigJob()
  12. output.clear() --clear print output area
  13. for n=1,10 do
  14. --time consuming code here
  15. for j=1,5000000 do local x=3.1^4.5 end
  16. print('completed',n)
  17. end
  18. print('Job done!')
  19. end
  20.  
  21. function bigJobCos()
  22. output.clear() --clear print output area
  23. progress=0 --used for printing status of job
  24. for n=1,10 do
  25. --time consuming code here
  26. for j=1,5000000 do local x=3.1^4.5 end
  27. progress=n
  28. coroutine.yield()
  29. end
  30. progress=-1 --signal end of job
  31. if cos then cos=nil end --just to be tidy, kill this off
  32. end
  33.  
  34. function draw()
  35. if progress then --only report if job is running, ie n is not nil
  36. if progress>0 then
  37. print('completed',progress) --print progress of job
  38. coroutine.resume(cos) --carry on calculating
  39. elseif progress==-1 then
  40. print('Job done!')
  41. progress=nil --to stop any more printing
  42. end
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement