UniTask.Run.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks
  5. {
  6. public partial struct UniTask
  7. {
  8. #region OBSOLETE_RUN
  9. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  10. public static UniTask Run(Action action, bool configureAwait = true, CancellationToken cancellationToken = default)
  11. {
  12. return RunOnThreadPool(action, configureAwait, cancellationToken);
  13. }
  14. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  15. public static UniTask Run(Action<object> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  16. {
  17. return RunOnThreadPool(action, state, configureAwait, cancellationToken);
  18. }
  19. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  20. public static UniTask Run(Func<UniTask> action, bool configureAwait = true, CancellationToken cancellationToken = default)
  21. {
  22. return RunOnThreadPool(action, configureAwait, cancellationToken);
  23. }
  24. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  25. public static UniTask Run(Func<object, UniTask> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  26. {
  27. return RunOnThreadPool(action, state, configureAwait, cancellationToken);
  28. }
  29. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  30. public static UniTask<T> Run<T>(Func<T> func, bool configureAwait = true, CancellationToken cancellationToken = default)
  31. {
  32. return RunOnThreadPool(func, configureAwait, cancellationToken);
  33. }
  34. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  35. public static UniTask<T> Run<T>(Func<UniTask<T>> func, bool configureAwait = true, CancellationToken cancellationToken = default)
  36. {
  37. return RunOnThreadPool(func, configureAwait, cancellationToken);
  38. }
  39. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  40. public static UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  41. {
  42. return RunOnThreadPool(func, state, configureAwait, cancellationToken);
  43. }
  44. [Obsolete("UniTask.Run is similar as Task.Run, it uses ThreadPool. For equivalent behaviour, use UniTask.RunOnThreadPool instead. If you don't want to use ThreadPool, you can use UniTask.Void(async void) or UniTask.Create(async UniTask) too.")]
  45. public static UniTask<T> Run<T>(Func<object, UniTask<T>> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  46. {
  47. return RunOnThreadPool(func, state, configureAwait, cancellationToken);
  48. }
  49. #endregion
  50. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  51. public static async UniTask RunOnThreadPool(Action action, bool configureAwait = true, CancellationToken cancellationToken = default)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. await UniTask.SwitchToThreadPool();
  55. cancellationToken.ThrowIfCancellationRequested();
  56. if (configureAwait)
  57. {
  58. try
  59. {
  60. action();
  61. }
  62. finally
  63. {
  64. await UniTask.Yield();
  65. }
  66. }
  67. else
  68. {
  69. action();
  70. }
  71. cancellationToken.ThrowIfCancellationRequested();
  72. }
  73. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  74. public static async UniTask RunOnThreadPool(Action<object> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  75. {
  76. cancellationToken.ThrowIfCancellationRequested();
  77. await UniTask.SwitchToThreadPool();
  78. cancellationToken.ThrowIfCancellationRequested();
  79. if (configureAwait)
  80. {
  81. try
  82. {
  83. action(state);
  84. }
  85. finally
  86. {
  87. await UniTask.Yield();
  88. }
  89. }
  90. else
  91. {
  92. action(state);
  93. }
  94. cancellationToken.ThrowIfCancellationRequested();
  95. }
  96. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  97. public static async UniTask RunOnThreadPool(Func<UniTask> action, bool configureAwait = true, CancellationToken cancellationToken = default)
  98. {
  99. cancellationToken.ThrowIfCancellationRequested();
  100. await UniTask.SwitchToThreadPool();
  101. cancellationToken.ThrowIfCancellationRequested();
  102. if (configureAwait)
  103. {
  104. try
  105. {
  106. await action();
  107. }
  108. finally
  109. {
  110. await UniTask.Yield();
  111. }
  112. }
  113. else
  114. {
  115. await action();
  116. }
  117. cancellationToken.ThrowIfCancellationRequested();
  118. }
  119. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  120. public static async UniTask RunOnThreadPool(Func<object, UniTask> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  121. {
  122. cancellationToken.ThrowIfCancellationRequested();
  123. await UniTask.SwitchToThreadPool();
  124. cancellationToken.ThrowIfCancellationRequested();
  125. if (configureAwait)
  126. {
  127. try
  128. {
  129. await action(state);
  130. }
  131. finally
  132. {
  133. await UniTask.Yield();
  134. }
  135. }
  136. else
  137. {
  138. await action(state);
  139. }
  140. cancellationToken.ThrowIfCancellationRequested();
  141. }
  142. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  143. public static async UniTask<T> RunOnThreadPool<T>(Func<T> func, bool configureAwait = true, CancellationToken cancellationToken = default)
  144. {
  145. cancellationToken.ThrowIfCancellationRequested();
  146. await UniTask.SwitchToThreadPool();
  147. cancellationToken.ThrowIfCancellationRequested();
  148. if (configureAwait)
  149. {
  150. try
  151. {
  152. return func();
  153. }
  154. finally
  155. {
  156. await UniTask.Yield();
  157. cancellationToken.ThrowIfCancellationRequested();
  158. }
  159. }
  160. else
  161. {
  162. return func();
  163. }
  164. }
  165. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  166. public static async UniTask<T> RunOnThreadPool<T>(Func<UniTask<T>> func, bool configureAwait = true, CancellationToken cancellationToken = default)
  167. {
  168. cancellationToken.ThrowIfCancellationRequested();
  169. await UniTask.SwitchToThreadPool();
  170. cancellationToken.ThrowIfCancellationRequested();
  171. if (configureAwait)
  172. {
  173. try
  174. {
  175. return await func();
  176. }
  177. finally
  178. {
  179. cancellationToken.ThrowIfCancellationRequested();
  180. await UniTask.Yield();
  181. cancellationToken.ThrowIfCancellationRequested();
  182. }
  183. }
  184. else
  185. {
  186. var result = await func();
  187. cancellationToken.ThrowIfCancellationRequested();
  188. return result;
  189. }
  190. }
  191. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  192. public static async UniTask<T> RunOnThreadPool<T>(Func<object, T> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  193. {
  194. cancellationToken.ThrowIfCancellationRequested();
  195. await UniTask.SwitchToThreadPool();
  196. cancellationToken.ThrowIfCancellationRequested();
  197. if (configureAwait)
  198. {
  199. try
  200. {
  201. return func(state);
  202. }
  203. finally
  204. {
  205. await UniTask.Yield();
  206. cancellationToken.ThrowIfCancellationRequested();
  207. }
  208. }
  209. else
  210. {
  211. return func(state);
  212. }
  213. }
  214. /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
  215. public static async UniTask<T> RunOnThreadPool<T>(Func<object, UniTask<T>> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
  216. {
  217. cancellationToken.ThrowIfCancellationRequested();
  218. await UniTask.SwitchToThreadPool();
  219. cancellationToken.ThrowIfCancellationRequested();
  220. if (configureAwait)
  221. {
  222. try
  223. {
  224. return await func(state);
  225. }
  226. finally
  227. {
  228. cancellationToken.ThrowIfCancellationRequested();
  229. await UniTask.Yield();
  230. cancellationToken.ThrowIfCancellationRequested();
  231. }
  232. }
  233. else
  234. {
  235. var result = await func(state);
  236. cancellationToken.ThrowIfCancellationRequested();
  237. return result;
  238. }
  239. }
  240. }
  241. }