Advertisement
Ultimga

Untitled

Nov 12th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. using System.Threading;
  2. using System.Collections;
  3.  
  4. namespace Swordfish
  5. {
  6. public class ThreadedJob : UnityEngine.ScriptableObject
  7. {
  8. public bool stop = false;
  9. public int updates = 0;
  10.  
  11. private bool m_IsDone = false;
  12. private object m_Handle = new object();
  13. private Thread m_Thread = null;
  14. public bool IsDone
  15. {
  16. get
  17. {
  18. bool tmp;
  19. lock (m_Handle)
  20. {
  21. tmp = m_IsDone;
  22. }
  23. return tmp;
  24. }
  25. set
  26. {
  27. lock (m_Handle)
  28. {
  29. m_IsDone = value;
  30. }
  31. }
  32. }
  33.  
  34. public virtual void Start()
  35. {
  36. stop = false;
  37. m_Thread = new Thread(Run);
  38. m_Thread.IsBackground = true;
  39. m_Thread.Start();
  40. }
  41.  
  42. public virtual void Abort()
  43. {
  44. stop = true;
  45. if (m_Thread != null)
  46. {
  47. m_Thread.Abort();
  48. }
  49. }
  50.  
  51. protected virtual void ThreadFunction() { }
  52.  
  53. protected virtual void OnFinished() { }
  54.  
  55. public virtual bool Update()
  56. {
  57. if (IsDone)
  58. {
  59. OnFinished();
  60. return true;
  61. }
  62. return false;
  63. }
  64.  
  65. // public IEnumerator WaitFor()
  66. // {
  67. // while(!Update())
  68. // {
  69. // yield return null;
  70. // }
  71. // }
  72.  
  73. private void Stop()
  74. {
  75. stop = true;
  76. }
  77.  
  78. private void Run()
  79. {
  80. ThreadFunction();
  81. IsDone = true;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement