UniTaskScheduler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // UniTask has no scheduler like TaskScheduler.
  7. // Only handle unobserved exception.
  8. public static class UniTaskScheduler
  9. {
  10. public static event Action<Exception> UnobservedTaskException;
  11. /// <summary>
  12. /// Propagate OperationCanceledException to UnobservedTaskException when true. Default is false.
  13. /// </summary>
  14. public static bool PropagateOperationCanceledException = false;
  15. #if UNITY_2018_3_OR_NEWER
  16. /// <summary>
  17. /// Write log type when catch unobserved exception and not registered UnobservedTaskException. Default is Exception.
  18. /// </summary>
  19. public static UnityEngine.LogType UnobservedExceptionWriteLogType = UnityEngine.LogType.Exception;
  20. /// <summary>
  21. /// Dispatch exception event to Unity MainThread. Default is true.
  22. /// </summary>
  23. public static bool DispatchUnityMainThread = true;
  24. // cache delegate.
  25. static readonly SendOrPostCallback handleExceptionInvoke = InvokeUnobservedTaskException;
  26. static void InvokeUnobservedTaskException(object state)
  27. {
  28. UnobservedTaskException((Exception)state);
  29. }
  30. #endif
  31. internal static void PublishUnobservedTaskException(Exception ex)
  32. {
  33. if (ex != null)
  34. {
  35. if (!PropagateOperationCanceledException && ex is OperationCanceledException)
  36. {
  37. return;
  38. }
  39. if (UnobservedTaskException != null)
  40. {
  41. #if UNITY_2018_3_OR_NEWER
  42. if (!DispatchUnityMainThread || Thread.CurrentThread.ManagedThreadId == PlayerLoopHelper.MainThreadId)
  43. {
  44. // allows inlining call.
  45. UnobservedTaskException.Invoke(ex);
  46. }
  47. else
  48. {
  49. // Post to MainThread.
  50. PlayerLoopHelper.UnitySynchronizationContext.Post(handleExceptionInvoke, ex);
  51. }
  52. #else
  53. UnobservedTaskException.Invoke(ex);
  54. #endif
  55. }
  56. else
  57. {
  58. #if UNITY_2018_3_OR_NEWER
  59. string msg = null;
  60. if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception)
  61. {
  62. msg = "UnobservedTaskException: " + ex.ToString();
  63. }
  64. switch (UnobservedExceptionWriteLogType)
  65. {
  66. case UnityEngine.LogType.Error:
  67. UnityEngine.Debug.LogError(msg);
  68. break;
  69. case UnityEngine.LogType.Assert:
  70. UnityEngine.Debug.LogAssertion(msg);
  71. break;
  72. case UnityEngine.LogType.Warning:
  73. UnityEngine.Debug.LogWarning(msg);
  74. break;
  75. case UnityEngine.LogType.Log:
  76. UnityEngine.Debug.Log(msg);
  77. break;
  78. case UnityEngine.LogType.Exception:
  79. UnityEngine.Debug.LogException(ex);
  80. break;
  81. default:
  82. break;
  83. }
  84. #else
  85. Console.WriteLine("UnobservedTaskException: " + ex.ToString());
  86. #endif
  87. }
  88. }
  89. }
  90. }
  91. }