AsyncTaskExecuter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Collections.Generic;
  12. using System;
  13. using System.Threading.Tasks;
  14. using System.Threading;
  15. /// <summary> Only works at Android runtime. </summary>
  16. public class AsyncTaskExecuter : SingleTon<AsyncTaskExecuter>
  17. {
  18. /// <summary> Queue of tasks. </summary>
  19. public Queue<Action> m_TaskQueue = new Queue<Action>();
  20. #if !UNITY_EDITOR
  21. public AsyncTaskExecuter()
  22. {
  23. Thread thread = new Thread(RunAsyncTask);
  24. thread.IsBackground = true;
  25. thread.Name = "AsyncTaskExecuter";
  26. thread.Start();
  27. }
  28. private void RunAsyncTask()
  29. {
  30. while (true)
  31. {
  32. Thread.Sleep(5);
  33. if (m_TaskQueue.Count != 0)
  34. {
  35. lock (m_TaskQueue)
  36. {
  37. var task = m_TaskQueue.Dequeue();
  38. try
  39. {
  40. task?.Invoke();
  41. }
  42. catch (Exception e)
  43. {
  44. NRDebugger.Error("[AsyncTaskExecuter] Execute async task error:" + e.ToString());
  45. throw;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. #endif
  52. /// <summary> Executes the action. </summary>
  53. /// <param name="task"> The task.</param>
  54. public void RunAction(Action task)
  55. {
  56. lock (m_TaskQueue)
  57. {
  58. #if !UNITY_EDITOR
  59. m_TaskQueue.Enqueue(task);
  60. #else
  61. task?.Invoke();
  62. #endif
  63. }
  64. }
  65. /// <summary> Executes a task witch has a timeout opration. </summary>
  66. /// <param name="task"> The task.</param>
  67. /// <param name="timeoutOpration"> The timeout opration.If the task does not time out, it is not
  68. /// executed.</param>
  69. /// <param name="delay"> The delay.</param>
  70. internal void RunAction(Action task, Action timeoutOpration, float delay)
  71. {
  72. var cancleToken = new CancellationTokenSource();
  73. if (delay > 0 && timeoutOpration != null)
  74. {
  75. Task.Factory.StartNew(async () =>
  76. {
  77. await Task.Delay((int)(delay * 1000));
  78. if (cancleToken.IsCancellationRequested)
  79. {
  80. return;
  81. }
  82. try
  83. {
  84. NRDebugger.Info("[AsyncTaskExecuter] Run action timeout...");
  85. timeoutOpration?.Invoke();
  86. }
  87. catch (Exception e)
  88. {
  89. NRDebugger.Error("[AsyncTaskExecuter] Run action timeout exeption:" + e.ToString());
  90. throw;
  91. }
  92. }, cancleToken.Token);
  93. }
  94. Task.Factory.StartNew(() =>
  95. {
  96. try
  97. {
  98. task?.Invoke();
  99. }
  100. catch (Exception)
  101. {
  102. throw;
  103. }
  104. cancleToken.Cancel();
  105. });
  106. }
  107. }
  108. }