LaunchThread.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using UnityEngine;
  6. public class LaunchThread : Singleton<LaunchThread>
  7. {
  8. public enum AsyncLaunchState
  9. {
  10. None,
  11. ConfigInit,
  12. LuaInit,
  13. }
  14. private Thread m_Thread = null;
  15. private Action m_AsyncAction = null;
  16. private Action m_UpdateAction = null;
  17. private Action m_CompleteAction = null;
  18. private int m_TimerSeqId = 0;
  19. private AsyncLaunchState m_AsyncLaunchState = AsyncLaunchState.None;
  20. ~LaunchThread()
  21. {
  22. Dispose();
  23. }
  24. public override void UnInit()
  25. {
  26. Dispose();
  27. }
  28. private void Dispose()
  29. {
  30. Stop();
  31. m_AsyncAction = null;
  32. m_UpdateAction = null;
  33. m_CompleteAction = null;
  34. }
  35. public bool Start(AsyncLaunchState asyncLaunchState, Action asyncAction, Action completeAction, Action updateAction)
  36. {
  37. if (m_Thread != null && m_Thread.IsAlive)
  38. {
  39. DebugHelper.LogError("The async thread is busy");
  40. return false;
  41. }
  42. Stop();
  43. m_AsyncLaunchState = asyncLaunchState;
  44. m_AsyncAction = asyncAction;
  45. m_UpdateAction = updateAction;
  46. m_CompleteAction = completeAction;
  47. m_TimerSeqId = TimerManager.Instance.AddTimer(0, -1, UpdateTimer);
  48. m_Thread = new Thread(ThreadProc);
  49. m_Thread.Start();
  50. return true;
  51. }
  52. public void Stop(AsyncLaunchState asyncLaunchState)
  53. {
  54. if (m_AsyncLaunchState != asyncLaunchState)
  55. {
  56. return;
  57. }
  58. Stop();
  59. }
  60. public void Stop()
  61. {
  62. try
  63. {
  64. if (m_Thread != null)
  65. {
  66. m_Thread.Abort();
  67. m_Thread.Join();
  68. }
  69. m_Thread = null;
  70. m_AsyncLaunchState = AsyncLaunchState.None;
  71. StopTimer();
  72. }
  73. catch (System.Threading.ThreadAbortException e)
  74. {
  75. DebugHelper.LogWarning(e.Message);
  76. }
  77. catch(Exception e)
  78. {
  79. DebugHelper.LogException(e);
  80. }
  81. }
  82. private void UpdateTimer(int timerSeqId)
  83. {
  84. try
  85. {
  86. if (m_UpdateAction != null)
  87. m_UpdateAction();
  88. if (m_Thread == null || !m_Thread.IsAlive)
  89. {
  90. Stop();
  91. if (m_CompleteAction != null)
  92. {
  93. m_CompleteAction();
  94. m_CompleteAction = null;
  95. }
  96. }
  97. }
  98. catch (System.Threading.ThreadAbortException e)
  99. {
  100. DebugHelper.LogWarning(e.Message);
  101. }
  102. catch(Exception e)
  103. {
  104. DebugHelper.LogException(e);
  105. }
  106. }
  107. private void StopTimer()
  108. {
  109. if (TimerManager.Instance != null)
  110. {
  111. TimerManager.Instance.RemoveTimer(m_TimerSeqId);
  112. }
  113. }
  114. private void ThreadProc()
  115. {
  116. try
  117. {
  118. if (m_AsyncAction != null)
  119. m_AsyncAction();
  120. }
  121. catch (System.Threading.ThreadAbortException e)
  122. {
  123. DebugHelper.LogWarning(e.Message);
  124. }
  125. catch(Exception e)
  126. {
  127. DebugHelper.LogException(e);
  128. }
  129. m_AsyncAction = null;
  130. }
  131. }