com.alicizax.unity.cysharp..../Runtime/UniTaskScheduler.cs
陈思海 0f01d0a681
Some checks are pending
Sync Github To Image / sync-gitlink (push) Waiting to run
Sync Github To Image / sync-gitlab (push) Waiting to run
Sync Github To Image / sync-gitee (push) Waiting to run
Sync Github To Image / sync-atomgit (push) Waiting to run
Sync Github To Image / sync-gitcode (push) Waiting to run
Sync Github To Image / sync-framagit (push) Waiting to run
init
2025-01-09 11:14:16 +08:00

104 lines
3.6 KiB
C#

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Threading;
namespace Cysharp.Threading.Tasks
{
// UniTask has no scheduler like TaskScheduler.
// Only handle unobserved exception.
public static class UniTaskScheduler
{
public static event Action<Exception> UnobservedTaskException;
/// <summary>
/// Propagate OperationCanceledException to UnobservedTaskException when true. Default is false.
/// </summary>
public static bool PropagateOperationCanceledException = false;
#if UNITY_2018_3_OR_NEWER
/// <summary>
/// Write log type when catch unobserved exception and not registered UnobservedTaskException. Default is Exception.
/// </summary>
public static UnityEngine.LogType UnobservedExceptionWriteLogType = UnityEngine.LogType.Exception;
/// <summary>
/// Dispatch exception event to Unity MainThread. Default is true.
/// </summary>
public static bool DispatchUnityMainThread = true;
// cache delegate.
static readonly SendOrPostCallback handleExceptionInvoke = InvokeUnobservedTaskException;
static void InvokeUnobservedTaskException(object state)
{
UnobservedTaskException((Exception)state);
}
#endif
public static void PublishUnobservedTaskException(Exception ex)
{
if (ex != null)
{
if (!PropagateOperationCanceledException && ex is OperationCanceledException)
{
return;
}
if (UnobservedTaskException != null)
{
#if UNITY_2018_3_OR_NEWER
if (!DispatchUnityMainThread || Thread.CurrentThread.ManagedThreadId == PlayerLoopHelper.MainThreadId)
{
// allows inlining call.
UnobservedTaskException.Invoke(ex);
}
else
{
// Post to MainThread.
PlayerLoopHelper.UnitySynchronizationContext.Post(handleExceptionInvoke, ex);
}
#else
UnobservedTaskException.Invoke(ex);
#endif
}
else
{
#if UNITY_2018_3_OR_NEWER
string msg = null;
if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception)
{
msg = "UnobservedTaskException: " + ex.ToString();
}
switch (UnobservedExceptionWriteLogType)
{
case UnityEngine.LogType.Error:
UnityEngine.Debug.LogError(msg);
break;
case UnityEngine.LogType.Assert:
UnityEngine.Debug.LogAssertion(msg);
break;
case UnityEngine.LogType.Warning:
UnityEngine.Debug.LogWarning(msg);
break;
case UnityEngine.LogType.Log:
UnityEngine.Debug.Log(msg);
break;
case UnityEngine.LogType.Exception:
UnityEngine.Debug.LogException(ex);
break;
default:
break;
}
#else
Console.WriteLine("UnobservedTaskException: " + ex.ToString());
#endif
}
}
}
}
}