AsyncTask.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace EZXR.Glass.Core.Threading
  5. {
  6. public class AsyncTask
  7. {
  8. public Thread Thread { get; protected set; }
  9. protected readonly object LockObject = new object();
  10. private readonly Queue<Action> _callBackQueue;
  11. private Queue<Action> CallBackQueue
  12. {
  13. get
  14. {
  15. lock (LockObject)
  16. {
  17. return _callBackQueue;
  18. }
  19. }
  20. }
  21. protected AsyncTask() { }
  22. public AsyncTask(Action action)
  23. {
  24. Thread thread = new Thread(() =>
  25. {
  26. action();
  27. });
  28. Thread = thread;
  29. Thread.Start();
  30. }
  31. public AsyncTask(Action action, int runFrequencyMs)
  32. {
  33. _callBackQueue = new Queue<Action>();
  34. Thread thread = new Thread(() =>
  35. {
  36. while (true)
  37. {
  38. action();
  39. CallBackQueue.Enqueue(action);
  40. Thread.Sleep(runFrequencyMs);
  41. }
  42. });
  43. Thread = thread;
  44. Thread.Start();
  45. }
  46. public bool IsFinished
  47. {
  48. get { return Thread.IsAlive == false; }
  49. }
  50. protected virtual bool IsBackgroundTask
  51. {
  52. get { return CallBackQueue != null; }
  53. }
  54. public void ContinueInMainThread(Action action)
  55. {
  56. _onTaskFinished = action;
  57. }
  58. private Action _onTaskFinished;
  59. public virtual void OnTaskFinished()
  60. {
  61. if (_onTaskFinished != null)
  62. {
  63. if (IsBackgroundTask)
  64. {
  65. while (CallBackQueue.Count > 0)
  66. {
  67. CallBackQueue.Dequeue();
  68. _onTaskFinished();
  69. }
  70. }
  71. else
  72. {
  73. _onTaskFinished();
  74. }
  75. }
  76. }
  77. }
  78. public class AsyncTask<T> : AsyncTask
  79. {
  80. public T Result { get; private set; }
  81. private readonly Queue<T> _backgroundResults;
  82. private Queue<T> BackgroundResults
  83. {
  84. get
  85. {
  86. lock (LockObject)
  87. {
  88. return _backgroundResults;
  89. }
  90. }
  91. }
  92. private Action<T> _onTaskFinishedResult;
  93. protected override bool IsBackgroundTask
  94. {
  95. get { return BackgroundResults != null; }
  96. }
  97. public AsyncTask(Func<T> func)
  98. {
  99. Thread thread = new Thread(() =>
  100. {
  101. Result = func();
  102. });
  103. Thread = thread;
  104. Thread.Start();
  105. }
  106. public AsyncTask(Func<T> func, int runFrequencyMs)
  107. {
  108. _backgroundResults = new Queue<T>();
  109. Thread thread = new Thread(() =>
  110. {
  111. while (true)
  112. {
  113. BackgroundResults.Enqueue(func());
  114. Thread.Sleep(runFrequencyMs);
  115. }
  116. });
  117. Thread = thread;
  118. Thread.Start();
  119. }
  120. public void ContinueInMainThread(Action<T> action)
  121. {
  122. _onTaskFinishedResult = action;
  123. }
  124. public override void OnTaskFinished()
  125. {
  126. if (_onTaskFinishedResult != null)
  127. {
  128. if (IsBackgroundTask)
  129. {
  130. while (BackgroundResults.Count > 0)
  131. {
  132. T result = BackgroundResults.Dequeue();
  133. _onTaskFinishedResult(result);
  134. }
  135. }
  136. else
  137. {
  138. _onTaskFinishedResult(Result);
  139. }
  140. }
  141. else base.OnTaskFinished();
  142. }
  143. }
  144. }