Advertisement
Ultimga

Untitled

Nov 12th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. using System.Threading;
  2. using UnityEngine;
  3.  
  4. namespace Swordfish {
  5. namespace threading
  6. {
  7.  
  8. public class ThreadWorker
  9. {
  10. private volatile bool stop = false;
  11. private volatile bool pause = false;
  12.  
  13. private Thread thread = null;
  14.  
  15. public ThreadWorker()
  16. {
  17. thread = new Thread(Tick);
  18. }
  19.  
  20. public void Start()
  21. {
  22. stop = false;
  23. pause = false;
  24. thread.Start();
  25. }
  26.  
  27. public void Stop()
  28. {
  29. stop = true;
  30. }
  31.  
  32. public void Restart()
  33. {
  34. stop = false;
  35. pause = false;
  36. thread.Abort();
  37. thread.Start();
  38. }
  39.  
  40. public void Pause()
  41. {
  42. pause = true;
  43. }
  44.  
  45. public void Unpause()
  46. {
  47. pause = false;
  48. }
  49.  
  50. public void TogglePause()
  51. {
  52. pause = !pause;
  53. }
  54.  
  55. public void Kill()
  56. {
  57. thread.Abort();
  58. }
  59.  
  60. private void Tick()
  61. {
  62. while (stop == false)
  63. {
  64. while (pause == false)
  65. {
  66. Run();
  67. }
  68.  
  69. Thread.Sleep(200); // Sleep when paused
  70. }
  71.  
  72. // Stopped thread safely
  73. }
  74.  
  75. public virtual void Run() {}
  76. }
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement