Files
Loong/Assets/Plugins/ThirdParty/AsyncAwaitUtil/Source/TaskExtensions.cs
2025-07-01 14:54:02 +08:00

41 lines
883 B
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using UnityEngine;
public static class TaskExtensions
{
public static IEnumerator AsIEnumerator(this Task task)
{
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
ExceptionDispatchInfo.Capture(task.Exception).Throw();
}
}
public static IEnumerator<T> AsIEnumerator<T>(this Task<T> task)
where T : class
{
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
ExceptionDispatchInfo.Capture(task.Exception).Throw();
}
yield return task.Result;
}
}